javax.ws.rs.NotFoundException:无法找到完整路径的资源

use*_*093 8 java resteasy

windows 7(64);

jdk1.7.0_51(64);

RESTEasy3.0.7;

Apache的Tomcat的7.0.50;

项目名称:你好

RESTEasyHelloWorldService.java:

Windows 7(64)
jdk1.7.0_51(64)
RESTEasy3.0.7
apache-tomcat-7.0.50
Project Name: hello
Run Code Online (Sandbox Code Playgroud)

web.xml中:

package com.javacodegeeks.enterprise.rest.resteasy;

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

@Path("/RESTEasyHelloWorld")
public class RESTEasyHelloWorldService {

    @GET
    @Path("/{param}")
    @Produces(MediaType.TEXT_PLAIN)
    public String getMsg(@PathParam("param") String name) {
        String msg = "Rest say: good " + name;
        return msg;
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么我在调用http://localhost:8080/hello/rest/RESTEasyHelloWorld/a 返回时得到异常:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>hello</display-name>

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

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

    <!-- this should be the same URL pattern as the servlet-mapping property -->
    <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>

</web-app>
Run Code Online (Sandbox Code Playgroud)

fas*_*fgs 6

你可以尝试使用http://localhost:8080/hello/RESTEasyHelloWorld/a.(没有/rest).

如果要使用/rest,可以将RESTEasyHelloWorldService @Path 修改为/rest/RESTEasyHelloWorld.


但基于您正在使用的API版本,您可以做一个更简单的工作来让您的宁静服务正常工作.

我假设你的类路径上有resteasy-jaxrs lib.

由于您不使用JBOSS或EAP,因此您还需要获得resteasy-servlet-initializer.这里使用像TOMCAT这样的Servlet 3.0容器的文档.

您将需要扩展Application,例如创建一个RESTEasyService:

@ApplicationPath("/rest")
public class RESTEasyService extends Application {
}
Run Code Online (Sandbox Code Playgroud)

您不需要为该类提供任何实现,因为RESTEasy将扫描所有提供程序和资源.在此处使用Application类的文档.

保留您的RESTEasyHelloWorldService就像您在问题上所说:

@Path("/RESTEasyHelloWorld")
public class RESTEasyHelloWorldService {

    @GET
    @Path("/{param}")
    @Produces(MediaType.TEXT_PLAIN)
    public String getMsg(@PathParam("param") String name) {
        String msg = "Rest say: good " + name;
        return msg;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在你的web.xml不需要任何东西.Java WS-RS和RESTEasy已经在做所有事情.

您的web.xml可以是这样的:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

    <display-name>hello</display-name>

</web-app>
Run Code Online (Sandbox Code Playgroud)

RESTEasy官方文档在开始时有点令人困惑,但是一旦你理解了JBOSS和NON-JBOSS应用程序的实现是相同的(只是改变了lib的使用),你就会变得容易.