Loading resources using Jersey and @ApplicationPath annotation

wulfgarpro picture wulfgarpro · Oct 7, 2012 · Viewed 12.7k times · Source

I'm trying to deploy a basic jersey restful service to Tomcat7 without web.xml:

 @WebServlet(loadOnStartup=1)
 @ApplicationPath("resources")
 @Path("/mypath/{name}")
 public class MyResource extends Application {

 @Override
 public Set<Class<?>> getClasses() {
     Set<Class<?>> s = new HashSet<Class<?>>();
     s.add(MyResource.class);
     return s;
 }

 @GET
 @Consumes("text/plain")
 @Produces("text/plain")
 public String getWelcome(@PathParam(value = "name") String name) {
     return "Welcome to jax-rs " + name;
 }
}

I'm presented with a 404 when trying to access: /myapplication/resources/mypath/sample.

I can deploy a servlet using the @WebServlet annotation, so this has nothing to do with the loading of servlets without web.xml into Tomcat7.

From reading the documentation for Jersey, the runtime should scan for classes extending Application and execute getClasses(), loading all root resources.

Answer

Martin Matula picture Martin Matula · Oct 7, 2012

Which version of Jersey are you using? Try splitting application and resource in two classes. Definitely remove @WebServlet annotation. I.e. have one class extending Application annotated with @ApplicationPath and another class annotated with @Path.

EDIT: Make sure that jersey-servlet.jar is included in your WAR file.