javax.ws.rs.NotFoundException:找不到完整路径的资源 [英] javax.ws.rs.NotFoundException: Could not find resource for full path

查看:79
本文介绍了javax.ws.rs.NotFoundException:找不到完整路径的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境

Windows 7(64)
jdk1.7.0_51(64)
RESTEasy3.0.7
apache-tomcat-7.0.50
Project Name: hello

RESTEasyHelloWorldService.java:

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;
    }
}

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>

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

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

why do I get the exception when I call the http://localhost:8080/hello/rest/RESTEasyHelloWorld/a returns:

javax.ws.rs.NotFoundException: Could not find resource for full path: http://localhost:8080/hello/rest/RESTEasyHelloWorld/a
    at org.jboss.resteasy.core.registry.ClassNode.match(ClassNode.java:73)
    at org.jboss.resteasy.core.registry.RootClassNode.match(RootClassNode.java:48)
...

推荐答案

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

You could try to use http://localhost:8080/hello/RESTEasyHelloWorld/a. (Without the /rest).

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

If you want to use /rest, you can modify your RESTEasyHelloWorldService @Path to /rest/RESTEasyHelloWorld.

但是根据您使用的API版本,您可以做得简单得多,以使您的静态服务正常工作.

But based on the APIs versions you are using, you can do a much simpler job to get your restful service working.

我假设您的类路径中有 resteasy-jaxrs 个库.

I'm assuming you have resteasy-jaxrs lib on your classpath.

由于您没有使用JBOSS或EAP,因此还需要获取 resteasy-servlet-initializer .使用 Servlet 3.0容器的文档,例如TOMCAT 此处.

Since you are not using JBOSS or EAP, you also need to get resteasy-servlet-initializer. Documentation for using Servlet 3.0 Containers like TOMCAT here.

您将需要扩展应用程序,例如,创建 RESTEasyService :

You will need to extend Application, creating for example a RESTEasyService:

@ApplicationPath("/rest")
public class RESTEasyService extends Application {
}

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

You don't need to provide any implementation for that class, since RESTEasy will scan for all providers and resources. Documentation for using Application class here.

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

Leave your RESTEasyHelloWorldService just like you said on your question:

@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;
    }
}

现在,您的web.xml不需要任何内容​​.Java WS-RS和RESTEasy已经在做所有事情.

Now your web.xml doesn't need anything. Java WS-RS and RESTEasy are already doing everything.

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

Your web.xml can be like this:

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

RESTEasy正式文档在开始时有点令人困惑,但是一旦您了解JBOSS和NON-JBOSS应用程序的实现是相同的(只是使用变化的库),就可以轻松进行.

RESTEasy official documentation is a little confusing at start, but once you understand that the implementation is the same for JBOSS and NON-JBOSS apps (just the use of libs that change), you get things going easier.

这篇关于javax.ws.rs.NotFoundException:找不到完整路径的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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