使用 Tomcat 的简单 RESTeasy 示例上的 404 响应 [英] 404 response on simple RESTeasy example with Tomcat

查看:40
本文介绍了使用 Tomcat 的简单 RESTeasy 示例上的 404 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个非常简单的hello world"服务与 Tomcat 和 RESTeasy 组合在一起.但是当我尝试测试它时,我得到的只是来自 Tomcat 的 404 响应.以下是我遵循的步骤,希望有人能指出我哪里出错了:

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. 在 Eclipse 中创建了一个新的动态 Web 项目.目标运行时设置为 Apache Tomcat 7.0,动态 Web 模块版本 3.0.

  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.

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

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

添加了一个类:

    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 中)

  1. Added the new project to Tomcat and restarted Tomcat (within Eclipse)

打开网络浏览器并请求/resteasy/restful-services/sampleservice/hello

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

这会从 Tomcat 返回一个带有描述的 404 响应:

This returns a 404 response from Tomcat with the description:

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

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

我错过了什么?

推荐答案

事实证明给定资源的 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>.

这篇关于使用 Tomcat 的简单 RESTeasy 示例上的 404 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆