javax.ws.rs.NotFoundException:找不到完整路径的资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25224581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javax.ws.rs.NotFoundException: Could not find resource for full path
提问by user3926093
Environment
环境
Windows 7(64)
jdk1.7.0_51(64)
RESTEasy3.0.7
apache-tomcat-7.0.50
Project Name: hello
RESTEasyHelloWorldService.java:
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:
网页.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>
why do I get the exception when I call the http://localhost:8080/hello/rest/RESTEasyHelloWorld/a
returns:
为什么在调用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)
...
回答by fasfsfgs
You could try to use http://localhost:8080/hello/RESTEasyHelloWorld/a
. (Without the /rest
).
你可以尝试使用http://localhost:8080/hello/RESTEasyHelloWorld/a
. (没有/rest
)。
If you want to use /rest
, you can modify your RESTEasyHelloWorldService@Path to /rest/RESTEasyHelloWorld
.
如果要使用/rest
,可以将RESTEasyHelloWorldService@Path修改为/rest/RESTEasyHelloWorld
.
But based on the APIs versions you are using, you can do a much simpler job to get your restful service working.
但是根据您使用的 API 版本,您可以做更简单的工作来让您的 Restful 服务正常工作。
I'm assuming you have resteasy-jaxrslib on your classpath.
我假设您的类路径上有resteasy-jaxrs库。
Since you are not using JBOSS or EAP, you also need to get resteasy-servlet-initializer. Documentation for using Servlet 3.0 Containerslike TOMCAT here.
由于您没有使用 JBOSS 或 EAP,您还需要获取resteasy-servlet-initializer。在此处使用Servlet 3.0 容器(如 TOMCAT)的文档。
You will need to extend Application, creating for example a RESTEasyService:
您将需要扩展Application,例如创建一个RESTEasyService:
@ApplicationPath("/rest")
public class RESTEasyService extends Application {
}
You don't need to provide any implementation for that class, since RESTEasy will scan for all providers and resources. Documentation for using Applicationclass here.
您不需要为该类提供任何实现,因为 RESTEasy 将扫描所有提供者和资源。在此处使用Application类的文档。
Leave your RESTEasyHelloWorldServicejust like you said on your question:
就像您在问题中所说的那样,离开您的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;
}
}
Now your web.xml doesn't need anything. Java WS-RS and RESTEasy are already doing everything.
现在您的 web.xml 不需要任何东西。Java WS-RS 和 RESTEasy 已经在做所有事情。
Your web.xml can be like this:
你的 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 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.
RESTEasy 官方文档在开始时有点令人困惑,但是一旦您了解 JBOSS 和非 JBOSS 应用程序的实现是相同的(只是使用更改的库),您就会变得更容易。
回答by Raphael
I got the same issue when I tried with 3.0.11.Final
当我尝试使用 3.0.11.Final 时遇到了同样的问题
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.11.Final</version>
</dependency>
but when I tried with another version it worked.
但是当我尝试使用另一个版本时它起作用了。
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.4.Final</version>
</dependency>
And moreover The URL(http://localhost:8080/hello/rest/RESTEasyHelloWorld/a) which you are trying is correct since you have mentioned /rest in the web.xml .Hope this helps.
此外,您正在尝试的 URL( http://localhost:8080/hello/rest/RESTEasyHelloWorld/a) 是正确的,因为您在 web.xml 中提到了 /rest 。希望这会有所帮助。
回答by temuchin
I had the same problem when I was migrating my app from resteasy version 3.0.4 to 3.0.12
当我将我的应用程序从 resteasy 版本 3.0.4 迁移到 3.0.12 时遇到了同样的问题
Web service was working fine with web.xml similar to one that user3926093 pasted above. What I released is that version 3.0.7 is changing point. Before that version you didn't even need resteasy-servlet-initializer as fasfsfgs stated above. But with 3.0.7 and later versions I started getting "Could not find resource for full path:" exception.
Web 服务使用 web.xml 工作正常,类似于上面 user3926093 粘贴的那个。我发布的是 3.0.7 版本正在改变点。在该版本之前,您甚至不需要上述 fasfsfgs 的 resteasy-servlet-initializer。但是在 3.0.7 及更高版本中,我开始收到“找不到完整路径的资源:”异常。
What I did in order to make it work is to change web.xml to look the same as fasfsfgs stated above (basically I removed all configuration from it) and I created subclass of javax.ws.rs.core.Application class also as fasfsfgs stated above but I don't agree that "You don't need to provide any implementation for that class". The way how you could implement this class could be found here: https://goo.gl/9TJ3Y2. Note that if you want per-request model than this implementation is not good for you. And lastly don't forget to add resteasy-servlet-initializer dependency.
为了使它工作,我所做的是将 web.xml 更改为与上述 fasfsfgs 相同(基本上我从中删除了所有配置),并且我创建了 javax.ws.rs.core.Application 类的子类也作为 fasfsfgs如上所述,但我不同意“您不需要为该类提供任何实现”。可以在此处找到如何实现此类的方法:https: //goo.gl/9TJ3Y2。请注意,如果您想要每个请求模型,那么此实现对您不利。最后不要忘记添加 resteasy-servlet-initializer 依赖项。