使用Jersey和@ApplicationPath批注加载资源 [英] Loading resources using Jersey and @ApplicationPath annotation
问题描述
我正在尝试在不使用web.xml的情况下将基本的jersey restful服务部署到Tomcat7:
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;
}
}
在尝试访问时,系统显示404错误:/myapplication/resources/mypath/sample .
I'm presented with a 404 when trying to access: /myapplication/resources/mypath/sample.
我可以使用@WebServlet
批注来部署servlet,因此,这与将没有web.xml的servlet加载到Tomcat7中无关.
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.
通过阅读Jersey的文档,运行时应扫描扩展Application
的类并执行getClasses()
,并加载所有根资源.
From reading the documentation for Jersey, the runtime should scan for classes extending Application
and execute getClasses()
, loading all root resources.
推荐答案
您正在使用哪个版本的Jersey?尝试将应用程序和资源分为两个类.绝对删除@WebServlet
批注. IE.有一个扩展了Application的类,该应用程序用@ApplicationPath
注释,另一个类是用@Path
注释.
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
.
确保您的WAR文件中包含jersey-servlet.jar
.
Make sure that jersey-servlet.jar
is included in your WAR file.
这篇关于使用Jersey和@ApplicationPath批注加载资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!