6

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.

wulfgarpro
  • 6,666
  • 12
  • 69
  • 110

1 Answers1

9

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.

Skylar Saveland
  • 11,116
  • 9
  • 75
  • 91
Martin Matula
  • 7,969
  • 1
  • 31
  • 35
  • I'm using v1.14 of Jersey. I tried splitting out the `Application` from the root resource class... to no avail. Having done that, I'm still trying to access the resource at the same URL. – wulfgarpro Oct 08 '12 at 08:36
  • 3
    Do you have jersey-servlet.jar in your war file? – Martin Matula Oct 08 '12 at 15:50
  • That fixed it. I don't understand, how is the Jersey specific servlet from jersey-servlet being referenced and loading my resources? – wulfgarpro Oct 09 '12 at 09:27
  • 1
    Using the servlet3 ServletContainerInitializer mechanism - Jersey registers a custom ServletContainerInitializer through META-INF services. This gets called by the servlet container if `@ApplicationPath` or `@Path` annotated classes are found and registers the custom Jersey servlet automatically. – Martin Matula Oct 09 '12 at 12:01
  • Thanks, that makes sense. So what is the custom Jersey servlet that's registered? Is that a servlet generated by Jersey after processing my root resource classes, provides, etc.? – wulfgarpro Oct 09 '12 at 12:46
  • It's `com.sun.jersey.spi.container.servlet.ServletContainer` with the URL mapping derived from the `@ApplicationPath` annotation value. – Martin Matula Oct 09 '12 at 14:35
  • What about additional configuration like true ? ... can i include them using annotation only? – Rafael Aug 01 '14 at 10:17
  • 1
    @MartinMatula you should add the `jersey-servlet.jar` hint to the answer. – sven Aug 26 '14 at 11:37
  • @MartinMatula when you say `jersey-servlet.jar` you mean `jersey-container-servlet-2.27.jar` and `jersey-container-servlet-core-2.27.jar`? – Michel Feinstein Apr 01 '19 at 21:38