【问题标题】:Loading resources using Jersey and @ApplicationPath annotation使用 Jersey 和 @ApplicationPath 注解加载资源
【发布时间】:2012-09-27 22:10:29
【问题描述】:

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

 @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

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

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

【问题讨论】:

标签: java tomcat jersey jax-rs servlet-3.0


【解决方案1】:

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

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

【讨论】:

  • 我使用的是泽西岛 v1.14。我尝试从根资源类中拆分出Application...无济于事。完成后,我仍在尝试访问同一 URL 的资源。
  • 你的war文件中有jersey-servlet.jar吗?
  • 修复了它。我不明白,来自 jersey-servlet 的 Jersey 特定 servlet 是如何被引用并加载我的资源的?
  • 使用 servlet3 ServletContainerInitializer 机制——Jersey 通过 META-INF 服务注册一个自定义的 ServletContainerInitializer。如果找到 @ApplicationPath@Path 带注释的类并自动注册自定义 Jersey servlet,则 servlet 容器将调用此方法。
  • @MartinMatula 您应该在答案中添加jersey-servlet.jar 提示。
猜你喜欢
  • 1970-01-01
  • 2011-02-25
  • 2011-12-20
  • 1970-01-01
  • 1970-01-01
  • 2012-11-30
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
相关资源
最近更新 更多