java 使用 Jersey 和 @ApplicationPath 注释加载资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12768514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:08:02  来源:igfitidea点击:

Loading resources using Jersey and @ApplicationPath annotation

javatomcatjerseyjax-rsservlet-3.0

提问by wulfgarpro

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

我正在尝试在没有 web.xml 的情况下向 Tomcat7 部署一个基本的 jersey restful 服务:

 @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.

我在尝试访问时看到 404:/myapplication/resources/mypath/sample

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

我可以使用@WebServlet注释部署一个servlet ,所以这与将没有web.xml的servlet加载到Tomcat7中无关。

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

通过阅读 Jersey 的文档,运行时应该扫描扩展Application和执行的类getClasses(),加载所有根资源。

回答by Martin Matula

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

您使用的是哪个版本的 Jersey?尝试将应用程序和资源分成两个类。绝对删除@WebServlet注释。即有一个扩展应用程序的@ApplicationPath类用注释,另一个类用注释@Path

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

编辑:确保jersey-servlet.jar包含在您的 WAR 文件中。