【问题标题】:Getting 404 error in Jersey with no web.xml在没有 web.xml 的 Jersey 中出现 404 错误
【发布时间】:2015-07-20 19:37:50
【问题描述】:

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

通过以下部分 - 示例 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 应用程序中没有任何web.xml 文件。现在我正在 Tomcat 7 服务器上部署我的应用程序。

当我尝试以 http://localhost:8080/myapp/resources/helloworld 访问 URL 时,出现错误

HTTP 状态 404 - /myapp/resources/helloworld 输入状态报告

消息:/myapp/resources/helloworld

说明:请求的资源不可用。

【问题讨论】:

    标签: java web-services rest tomcat jersey


    【解决方案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

    【讨论】:

    • 非常感谢,它成功了。请检查我的另一个问题:stackoverflow.com/questions/31522824/…
    • 我需要一个帮助,官方文档没有提到在pom.xml文件中添加这个依赖,所以我觉得文档有些不完整。我应该继续使用相同的用户指南学习,还是有其他更好的文档或书籍可以用来学习泽西岛。请提出建议。
    • 嗯,有可能部分文档在 1.10 版之后没有更新。那是 servlet 工件被拆分的时候。我认为即使在新泽西州 2.x 中,它也没有提及我在上面发布的人工制品。甚至所有官方的例子仍然使用xml,这里只使用了jersey-container-servlet-core。这对于没有 web.xml 是不够的。你需要上面那个。出于学习目的,这取决于您要学习的版本。如果要入手2.x,可以看here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    相关资源
    最近更新 更多