wulfgarpro wulfgarpro - 1 month ago 31
Java Question

Loading resources using Jersey and @ApplicationPath annotation

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 Source

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.