首页 文章

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

提问于
浏览
1

我正在关注Jersey tutorial开发简单的Jersey Web应用程序 .

按照以下部分 - Example 2.9. Deployment of a JAX-RS application using @ApplicationPath with Servlet 3.0

我创建了以下程序:

@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应用程序中没有任何web.xml文件 . 现在我在Tomcat 7服务器上部署我的应用程序 .

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

HTTP状态404 - / myapp / resources / helloworld类型状态报告消息:/ myapp / resources / helloworld description:请求的资源不可用 .

1 回答

  • 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

相关问题