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
Loading resources using Jersey and @ApplicationPath annotation
提问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 @WebServlet
annotation, 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 Application
and 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 @WebServlet
annotation. I.e. have one class extending Application annotated with @ApplicationPath
and another class annotated with @Path
.
您使用的是哪个版本的 Jersey?尝试将应用程序和资源分成两个类。绝对删除@WebServlet
注释。即有一个扩展应用程序的@ApplicationPath
类用注释,另一个类用注释@Path
。
EDIT: Make sure that jersey-servlet.jar
is included in your WAR file.
编辑:确保jersey-servlet.jar
包含在您的 WAR 文件中。