簡體   English   中英

javax.ws.rs.NotFoundException:找不到完整路徑的資源

[英]javax.ws.rs.NotFoundException: Could not find resource for full path

環境

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

網頁.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返回時會出現異常:

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

如果要使用/rest ,可以將RESTEasyHelloWorldService @Path 修改為/rest/RESTEasyHelloWorld


但是根據您使用的 API 版本,您可以做更簡單的工作來讓您的 Restful 服務正常工作。

我假設您的類路徑上有resteasy-jaxrs庫。

由於您沒有使用 JBOSS 或 EAP,您還需要獲取resteasy-servlet-initializer 此處使用Servlet 3.0 容器(如 TOMCAT)的文檔。

您將需要擴展Application ,例如創建一個RESTEasyService

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

您不需要為該類提供任何實現,因為 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;
    }
}

現在您的 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>

RESTEasy 官方文檔在開始時有點令人困惑,但是一旦您了解 JBOSS 和非 JBOSS 應用程序的實現是相同的(只是使用更改的庫),您就會變得更容易。

當我嘗試使用 3.0.11.Final 時遇到了同樣的問題

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.11.Final</version>
</dependency>

但是當我嘗試使用另一個版本時它起作用了。

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.4.Final</version>
</dependency>

此外,您正在嘗試的 URL( http://localhost:8080/hello/rest/RESTEasyHelloWorld/a ) 是正確的,因為您在 web.xml 中提到了 /rest 。希望這會有所幫助。

當我將我的應用程序從 resteasy 版本 3.0.4 遷移到 3.0.12 時遇到了同樣的問題

Web 服務使用 web.xml 工作正常,類似於上面 user3926093 粘貼的那個。 我發布的是 3.0.7 版本正在改變點。 在該版本之前,您甚至不需要上述 fasfsfgs 的 resteasy-servlet-initializer。 但是在 3.0.7 及更高版本中,我開始收到“找不到完整路徑的資源:”異常。

為了使它工作,我所做的是將 web.xml 更改為與上述 fasfsfgs 相同(基本上我從中刪除了所有配置),並且我創建了 javax.ws.rs.core.Application 類的子類也作為 fasfsfgs如上所述,但我不同意“您不需要為該類提供任何實現”。 可以在此處找到如何實現此類的方法: https : //goo.gl/9TJ3Y2 請注意,如果您想要每個請求模型,那么此實現對您不利。 最后不要忘記添加 resteasy-servlet-initializer 依賴項。

I got the same issue when I tried with **3.13.2.Final** but when I tried with **3.6.3.Final** it worked.

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.6.3.Final</version>
</dependency>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM