在没有web.xml的情况下在Jersey中获得404错误

时间:2015-07-20 19:37:51

标签: java web-services rest tomcat jersey

我正在关注Jersey tutorial开发简单的Jersey网络应用程序。

通过以下部分 - 示例2.9。使用@ApplicationPath和Servlet 3.0部署JAX-RS应用程序

我创建了以下程序:

@ApplicationPath("resources")
public class MyApplication extends PackagesResourceConfig {
    public MyApplication() {
        super("com.examples");
    }
}

我有以下基本资源类:

@Path("/helloworld")
public class HelloWorldResource {

    @GET
    @Produces("text/plain")
    public String getClichedMessage() {
        return "Hello World";
    }
 }

我使用的是Jersey-1.19版本,我的网络应用程序中没有任何web.xml文件。现在我在Tomcat 7服务器上部署我的应用程序。

当我尝试访问网址时:http://localhost:8080/myapp/resources/helloworld我收到错误

  

HTTP状态404 - / myapp / resources / helloworld   类型状态报告

     消息:/ myapp / resources / helloworld

     

description:请求的资源不可用。

1 个答案:

答案 0 :(得分:1)

您需要jersey-servlet依赖

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-servlet</artifactId>
    <version>1.19</version>
</dependency>

如果你想没有web.xml。它具有加载应用程序所需的JerseyServletContainerInitializer

对于任何遇到这种寻找Jersey 2.x解决方案的未来读者而言,您需要以下依赖关系才能使用无web.xml

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>${jersey2.version}</version>
</dependency>

出于同样的原因 - 它有JerseyServletContainerInitializer

相关问题