使用 Tomcat 的简单 RESTeasy 示例的 404 响应

发布于 2025-01-08 03:46:03 字数 2619 浏览 1 评论 0原文

我正在尝试使用 Tomcat 和 RESTeasy 组合一个非常简单的“hello world”服务。但是当我尝试测试它时,我得到的只是 Tomcat 的 404 响应。以下是我遵循的步骤,希望有人能指出我出错的地方:

  1. 在 Eclipse 中创建了一个新的动态 Web 项目。目标运行时设置为 Apache Tomcat 7.0、动态 Web 模块版本 3.0。

  2. 将resteasy-jaxrs-2.3.1.GA-all.zip中的所有jar文件复制到WEB-INF\lib

  3. 添加了一个类:

    package com.eshayne.resteasy;

    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    @Path("/sampleservice")
    public class SampleService {
        @GET
        @Path("/hello")
        @Produces(MediaType.TEXT_HTML)
        public String hello()
        {
            return "hello world";
        }
    }
  1. web.xml 设置为:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true">
        <display-name>resteasy</display-name>

        <listener>
            <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
        </listener>

        <servlet>
            <servlet-name>Resteasy</servlet-name>
            <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
        </servlet>

        <servlet-mapping>
            <servlet-name>Resteasy</servlet-name>
            <url-pattern>/restful-services/*</url-pattern>
        </servlet-mapping>

        <context-param>
            <param-name>resteasy.scan</param-name>
            <param-value>true</param-value>
        </context-param>

        <context-param>
            <param-name>resteasy.servlet.mapping.prefix</param-name>
            <param-value>/restful-services</param-value>
        </context-param>
    </web-app>
  1. 将新项目添加到 Tomcat 并重新启动Tomcat(在 Eclipse 中)

  2. 打开 Web 浏览器并请求 /resteasy/restful-services/sampleservice/hello

这会从 Tomcat 返回 404 响应,其描述为:

请求的资源(/resteasy/restful-services/sampleservice/hello)不可用。

我缺少什么?

I am trying to put together a very simple "hello world" service with Tomcat and RESTeasy. But when I try to test it, all I get are 404 responses from Tomcat. Here are the steps I followed, hopefully someone can point out where I went wrong:

  1. Created a new Dynamic Web Project in Eclipse. The Target Runtime is set for Apache Tomcat 7.0, Dynamic web module version 3.0.

  2. Copied all of the jar files from resteasy-jaxrs-2.3.1.GA-all.zip into WEB-INF\lib

  3. Added one class:

    package com.eshayne.resteasy;

    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    @Path("/sampleservice")
    public class SampleService {
        @GET
        @Path("/hello")
        @Produces(MediaType.TEXT_HTML)
        public String hello()
        {
            return "hello world";
        }
    }
  1. Set web.xml to:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true">
        <display-name>resteasy</display-name>

        <listener>
            <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
        </listener>

        <servlet>
            <servlet-name>Resteasy</servlet-name>
            <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
        </servlet>

        <servlet-mapping>
            <servlet-name>Resteasy</servlet-name>
            <url-pattern>/restful-services/*</url-pattern>
        </servlet-mapping>

        <context-param>
            <param-name>resteasy.scan</param-name>
            <param-value>true</param-value>
        </context-param>

        <context-param>
            <param-name>resteasy.servlet.mapping.prefix</param-name>
            <param-value>/restful-services</param-value>
        </context-param>
    </web-app>
  1. Added the new project to Tomcat and restarted Tomcat (within Eclipse)

  2. Opened a web browser and requested /resteasy/restful-services/sampleservice/hello

This returns a 404 response from Tomcat with the description:

The requested resource (/resteasy/restful-services/sampleservice/hello) is not available.

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怀里藏娇 2025-01-15 03:46:04

您是否尝试过访问 URL 的变体 - 例如 /restful-services/sampleservice/hello

Have you tried visiting variations on the URL - e.g. /restful-services/sampleservice/hello

紫南 2025-01-15 03:46:04

事实证明,给定资源的 URL 实际上不是 ///。它实际上是 ///

It turns out that the URL for a given resource is not in fact <display-name>/<servlet-mapping>/<class Path>/<method Path>. It is actually <project-name>/<servlet-mapping>/<class Path>/<method Path>.

不弃不离 2025-01-15 03:46:04

引用RESTEasy教程:访问资源的URL由

<web context>/<class level @Path>/<Method level @Path>

For例如,如果您部署 demo.war,它看起来像这样:

http://localhost:8080/demo/sampleservice/hello

并且 web.xml 中必须有一个 servlet-mapping,例如:

<servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/sampleservice/*</url-pattern>
</servlet-mapping>

A quote from RESTEasy tutorial: The URL to access a resource consists of

<web context>/<class level @Path>/<Method level @Path>

For example, if you deploy demo.war, it looks like this:

http://localhost:8080/demo/sampleservice/hello

And there must be a servlet-mapping in web.xml, for example:

<servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/sampleservice/*</url-pattern>
</servlet-mapping>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文