在没有 web.xml 的 Jersey 中出现 404 错误

Getting 404 error in Jersey with no web.xml

我正在跟随 Jersey tutorial 开发简单的 Jersey Web 应用程序。

通过以下部分 - 示例 2.9。使用@ApplicationPath 和 Servlet 3.0

部署 JAX-RS 应用程序

我创建了以下程序:

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

我有以下基本资源 class:

@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 Status 404 - /myapp/resources/helloworld type Status report

message: /myapp/resources/helloworld

description: The requested resource is not available.

您需要 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