【问题标题】:I get 404 while trying to hit a resteasy service我在尝试使用 Resteasy 服务时收到 404
【发布时间】:2014-05-12 12:10:51
【问题描述】:

我已经下载了一个源代码并尝试从其他客户端访问它,但我收到 404 错误。 当我测试我的本地服务器时,我得到了 200 OK。 我错过了什么?谁能解释一下调用服务的步骤? 以下是该项目的详细信息:

Web.xml

    <web-app id="WebApp_ID" version="2.4"
        xmlns="http://java.sun.com/xml/ns/j2ee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
        <display-name>Restful Web Application</display-name>

        <!-- Auto scan REST service -->
        <context-param>
            <param-name>resteasy.scan</param-name>
            <param-value>true</param-value>
        </context-param>

        <!-- this need same with resteasy servlet url-pattern -->
        <context-param>
            <param-name>resteasy.servlet.mapping.prefix</param-name>
            <param-value>/rest</param-value>
        </context-param>

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

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

        <servlet-mapping>
            <servlet-name>resteasy-servlet</servlet-name>
            <url-pattern>/rest/*</url-pattern>
        </servlet-mapping>

    </web-app>

服务类

package com.mkyong.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;



    @Path("/message")
    public class MessageRestService {

        @GET
        @Path("/{param}")
        public Response printMessage(@PathParam("param") String msg) {

            String result = "Restful example : " + msg;

            return Response.status(200).entity(result).build();

        }

    }

【问题讨论】:

    标签: java tomcat7 resteasy


    【解决方案1】:

    上面的服务配置是使用下面的url调用的:

    GET http://{host}:{port}/rest/message/{parameter}
    

    此 url 模式的一个示例是(请记住,您的主机名和端口可能会根据您运行示例的方式而有所不同):

    GET http://localhost:8080/rest/message/my+message
    

    上面的 url 假设您的应用程序的 Web 上下文根为 /。您尚未提供运行配置,因此我无法确定您正在使用的上下文根目录。上下文根是应用程序的基本 url。

    如果您的上下文根与 / 不同,您的 url 将如下所示:

    GET http://{host}:{port}/{context root}/rest/message/{parameter}
    

    我相信在 mykong 示例中,他使用 RESTfulExample 作为他的 maven 配置中的上下文根。这意味着该网址将是:

    GET http://localhost:8080/RESTfulExample/rest/message/my+message
    

    【讨论】:

    • GET localhost:8080/RESTfulExample/rest/message/mykong... 我试过这个仍然得到 404
    • 你是如何运行这个例子的?在教程中的maven中还是在tomcat中,如上面的标签所示?如果你在 tomcat 中运行,你很可能没有正确配置上下文根。
    猜你喜欢
    • 2022-07-04
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    相关资源
    最近更新 更多