Jersey 1.0 and jetty:run
If you are using maven and jetty you are probably simply invoking jetty:run with your Jersey web service.
Jersey is the JAX-RS (JSR 311) Reference Implementation for building RESTful Web services
The problem is you get this exception:
1 |
The ResourceConfig instance does not contain any root resource classes. |
But you are sure you do have your resources. Here is an example:
1 2 3 4 5 6 7 8 |
@Path("/rocky/") public class RockyResource { @GET @Produces("text/plain") public String yo() { return "Yo Adrian!"; } } |
In pre 1.0 releases you used the annotations @ProduceMime(“text/plain”), @UriTemplate(“/rocky/”) and @HttpMethod(“GET”). These are the replacements for 1.0.
You think that Rest is so hip and happenin’ that other people have had the same problem so you google the exception.
You then find this blog post. It was helpful but that was a preview release.
Here is a web.xml that will now work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Jersey test</display-name> <servlet> <servlet-name>ServletContainer</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <!-- multiple packages separated by ; --> <param-value>com.rockhoppertech.jerseyServer</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ServletContainer</servlet-name> <url-pattern>/resources/*</url-pattern> </servlet-mapping> </web-app> |
See the Glassfish Wiki for details.
Here are the javadocs for the JSR. Since I’m posting links, if you are new to JSON here are some docs.
If you created your maven app with the Jersey teams maven archetype then this wouldn’t have been a problem. You’d just have to delete stuff from the pom and insert the jetty stuff.
1 |
mvn archetype:generate -DarchetypeCatalog=http://download.java.net/maven/2 |