Loading resources using Jersey and @ApplicationPath annotation

怎甘沉沦 提交于 2019-12-01 02:24:38

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/12768514/loading-resources-using-jersey-and-applicationpath-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!