Jersey REST ResourceConfig 实例不包含任何根资源类
尽管这是一个古老的问题,但我仍然找不到使这项工作可行的答案。 如果您发现我的说法有不正确的地方,请指正。
我有一个 Java Face 应用程序并使用 REST 作为 Web 服务。我不认为 Face 与我的问题有任何关系。 web.xml 是:
<servlet>
<servlet-name>NDREST</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.bi.nd.webservice</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>NDREST</servlet-name>
<url-pattern>/nd/*</url-pattern>
</servlet-mapping>
我在 web.xml 中有更多的 servlet,因为它是 Trinidad 等的 Face 应用程序。
在 com.bi.nd.webservice 包中,我的资源类是:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Produces(MediaType.APPLICATION_XML)
@Path("/hello")
public class TransactionResource
{
public TransactionResource()
{
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public String itWorks()
{
return "Get is OK";
}
}
事实上,我的类有一个 @GET 足以标识自己是一个资源类。
更不用说所有其他复杂性了,我在 Eclipse 中使用 Ant 编译了源代码,并且在 catalina.out 文件中遇到了此错误:
May 24, 2011 8:48:46 AM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
com.bi.nd.webservice
May 24, 2011 8:48:46 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.7 05/20/2011 11:04 AM'
May 24, 2011 8:48:46 AM com.sun.jersey.server.impl.application.RootResourceUriRules <init>
SEVERE: The ResourceConfig instance does not contain any root resource classes.
有人建议复制 asm.jar、jsr-api.jar、jersey-server.jar 和 jersey- core.jar 到应用程序 WEB-INF/lib 中。我这样做了,但仍然没有成功。 我发现这个建议有点奇怪,因为 WEB-INF/lib 是 Eclipse 将从构建路径安装所有依赖库的地方。它不是我们手动放置库的地方。
有人解释说这个错误与Java插件和Jersey的编写方式有关。但那是几年前的事了。
有人可以向我解释一下为什么我一直遇到这个问题吗?
后续: 尽管从 REST 网站来看,定义为根资源类的资源类是 POJO(普通旧 Java 对象),它们要么用 @Path 注释,要么具有至少一个用 @Path 注释的方法或请求方法指示符,例如 @GET、@PUT 、@POST 或@DELETE。资源方法是用请求方法指示符注释的资源类的方法。本节介绍如何使用 Jersey 注释 Java 对象以创建 RESTful Web 服务。
我必须将 @Path("/hello") 添加到我的类中,突然 Jersey 可以找到我的资源类
现在 catalina.out 文件看起来像:
May 24, 2011 3:13:02 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
com.bi.nd.webservice
May 24, 2011 3:13:02 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
class com.bi.nd.webservice.TransactionResource
May 24, 2011 3:13:02 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
May 24, 2011 3:13:02 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.7 05/20/2011 11:04 AM'
但问题还远未结束。我尝试访问 URL http://localhost:8080/nd/hello
但仍然收到 404 NOT FOUND。 Provider 类 NOT FOUND 是重要消息吗?
Although this is one ancient question, I still can not find the answer to make this work.
Please correct if you find any of my statement is not correct.
I have a Java Face app and use REST for the Web Services. I don't think Face has anything to do with my problem at all.
The web.xml is:
<servlet>
<servlet-name>NDREST</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.bi.nd.webservice</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>NDREST</servlet-name>
<url-pattern>/nd/*</url-pattern>
</servlet-mapping>
I have quite a few more servlets in the web.xml since is is a Face app with Trinidad, etc.
In the package com.bi.nd.webservice, my resource class is:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Produces(MediaType.APPLICATION_XML)
@Path("/hello")
public class TransactionResource
{
public TransactionResource()
{
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public String itWorks()
{
return "Get is OK";
}
}
The fact that my class has a @GET is enough to identify itself a resource class.
Let alone all other complexities, I compiled my source code with Ant in Eclipse and I got this error in the catalina.out file:
May 24, 2011 8:48:46 AM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
com.bi.nd.webservice
May 24, 2011 8:48:46 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.7 05/20/2011 11:04 AM'
May 24, 2011 8:48:46 AM com.sun.jersey.server.impl.application.RootResourceUriRules <init>
SEVERE: The ResourceConfig instance does not contain any root resource classes.
Some suggested that copy the asm.jar, jsr-api.jar, jersey-server.jar and jersey-core.jar into the app WEB-INF/lib. I did that and it still did not work.
I found the suggestion was a bit odd since WEB-INF/lib is a place where Eclipse will install all dependency libraries from the build path. It is not a place where we manually put libraries.
Some explained that this error had something to do with Java plug-in and the way Jersey was written. But that was years ago.
Can some one explains to me why I keep having this problem?
Followup:
Although from REST web site, resource class defined as Root resource classes are POJOs (Plain Old Java Objects) that are either annotated with@Path or have at least one method annotated with @Path or a request method designator such as @GET, @PUT, @POST, or @DELETE. Resource methods are methods of a resource class annotated with a request method designator. This section describes how to use Jersey to annotate Java objects to create RESTful web services.
I have to add @Path("/hello") to my class, and suddenly Jersey can find my resource class
Now the catalina.out file looks like:
May 24, 2011 3:13:02 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
com.bi.nd.webservice
May 24, 2011 3:13:02 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
class com.bi.nd.webservice.TransactionResource
May 24, 2011 3:13:02 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
May 24, 2011 3:13:02 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.7 05/20/2011 11:04 AM'
But the problem is far from over. I try to access the URL http://localhost:8080/nd/hello
and I still get the 404 NOT FOUND. Is the Provider class NOT FOUND an important message?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我有同样的错误消息并通过修改我的 web.xml 文件解决了它。确保它里面有这个:
I had the same error message and solved it by modifying my web.xml file. Make sure it has this in it:
我发现了 Jersey 无法找到根资源的另一个原因。错误消息是相同的,所以我认为这是在这里记录根本原因的一个很好的理由,以防其他人偶然发现它。
我有一个根资源类,上面有以下注释:
这将导致 Jersey 内的根资源扫描失败。
解决方法是更改注释的顺序:
这有效。
I found another reason Jersey could fail to find a root resource. The error message is the same, so I thought it'd be a good reason to document the root cause here in case other people stumble on it.
I have a root resource class that has the following annotations on it:
This will cause the root resource scanning within Jersey to fail.
The fix is to change the order of the annotations:
This works.
首先,请注意,在类上使用 @GET 不足以将其标识为根资源类;该类必须有一个@Path。你的确实如此,所以这不是问题。
我遇到了和你一样的问题:它在 Eclipse 中工作,但当我构建它供外部使用时却不起作用。
我正在使用嵌入式 Jetty,其 main() 如下所示:(
嵌入 Jetty 时,它取代了 web.xml。) com.sun.jersey.api.core.PackagesResourceConfig 扫描指定类的根目录资源提供者类——我确信 web.xml 只是指向相同的。这在 Eclipse 中运行良好。服务器启动在控制台中正确地报告了这一点:
但是,当我在服务器上启动应用程序时,我只得到了前两行,并且没有找到根资源类。
事实证明,当你制作一个罐子时,它的构造方式有不同的选择;特别是,如果您只是通过列出类的路径来实现它,您会得到
只是那些条目。但是,如果您将 jar 指向带有通配符的目录,您还将获得所有中间路径。请参阅此消息,了解给我提示的示例:https://groups.google.com/d/msg /neo4j/0dNqGXvEbNg/xaNlRiU1cHMJ 。
PackagesResourceConfig 类依赖于具有包名称的目录,以便查找其中的类。我返回到 Eclipse,并在“Export ... > Jar”对话框的底部找到了一个用于“添加目录条目”的选项。我打开它,再次导出 jar,然后扫描找到了我的资源类。您需要在构建环境中找到一些类似的选项。
Firstly, note that having @GET on a class is not enough to identify it as a root resource class; the class must have an @Path on it. Yours does, so that's not the problem.
I was having the same problem as you: it worked in Eclipse, but then didn't work when I built it for external use.
I'm using embedded Jetty, with a main() that looks like this:
(When embedding Jetty, this takes the place of a web.xml.) com.sun.jersey.api.core.PackagesResourceConfig scans the named classes for root resource provider classes -- I'm sure the web.xml is just pointing at the same. This worked fine in Eclipse. Server startup correctly reports this in the console:
However, when I start the application on a server, I only get the first two lines of that, and no Root resource classes are found.
It turns out that when you make a jar, there are different options for how it is constructed; in particular, if you just make it by listing the paths to the classes, you get
just those entries. But if you point jar at the directory with a wild card, you also get all the intermediate paths. See this message for the example that tipped me off: https://groups.google.com/d/msg/neo4j/0dNqGXvEbNg/xaNlRiU1cHMJ .
The PackagesResourceConfig class depends on having a directory with the name of the package in order to find the classes within it. I went back to Eclipse, and found an option at the bottom of the "Export ... > Jar" dialog for "Add directory entries." I turned that on, exported the jar again, and then the scanning found my resource classes. You'll need to find some comparable option in your build environment.
我也花了 5 或 6 个小时来解决这个问题,终于解决了这个问题。这也许对你也有用。
我一直在使用的源代码可以在 Lars Vogel 的教程页面 中找到。使用 Apache Tomcat 6.0.20、asm-all-3.3.1.jar、jersey-bundle-1.17.1.jar 和 jsr311-api-1.1.1.jar 我得到了与OP相同的结果,即
我有各种部署文件中的以下设置:
尝试使用以下 URL 访问资源时出现 404 错误,
我最终能够通过将 url-pattern 更改为来修复它:
虽然我不确定如何当我开始引入更复杂的资源时,这一点将会成立。无论如何,希望这可以帮助某人。
I've just spent a good 5 or 6 hours wrestling with this too and have finally fixed the problem. This may work for you too perhaps.
The source code I've been using is available at Lars Vogel's tutorial page. Using Apache Tomcat 6.0.20, asm-all-3.3.1.jar, jersey-bundle-1.17.1.jar and jsr311-api-1.1.1.jar I was getting the same results as the OP, i.e.
I had the following settings across the various deployment files:
Trying to access the resource using the following URL gave a 404 error
I was eventually able to fix it by changing the url-pattern to :
Though I'm not sure how this will hold up when I start to introduce more complex resources. Anyway, hopefully this may help someone.
我发现“未找到提供程序类”消息很可怕,但并不重要。
“ResourceConfig 实例不包含任何根资源类”是一个更大的问题。
我发现资源类需要用@Path注释,而提供者类(如果有的话——我们使用一个用于将异常映射到HTTP响应代码)需要用@Provider注释,并且两者都需要在正确的包裹,然后 Jersey 就会找到它们。
I found that the "no provider classes found" message is scary but not important.
"The ResourceConfig instance does not contain any root resource classes" is a much bigger problem.
I found that the resource class needs to be annotated with @Path, and the provider class (if you have one -- we use one for mapping exceptions to HTTP response codes) needs to be annotated with @Provider, and both need to be in the right package, and then Jersey will find them.
你可能需要使用这样的网址
you might need to use a url like this
所以我读了答案,但仍然遇到同样的问题。我解决如下。
将“jetty-web.xml”添加到“WEB-INF”文件夹中。因此:
src/main/webapp/WEB-INF/jetty-web.xml jetty
-web.xml 的内容是:
我的休息现在可以在:
希望这对那里的人有帮助。
So I read trough the answers and still had the same problem. I resolved it as follows.
Added "jetty-web.xml" to the "WEB-INF" folder. Thus:
src/main/webapp/WEB-INF/jetty-web.xml
The content of the jetty-web.xml is:
My rest is now available at:
Hope this helps someone out there.
我也有同样的问题。通过在 web.xml 中修复 init-param 标记下的 param-value 解决了这个问题。
默认情况下,Eclipse已在web.xml中将项目名称设置为display-name。不应将其复制到 param-value,而是复制到 Java 包。
希望这有帮助。
I had the same problem. Solved it by fixing in the web.xml the param-value under init-param tag.
By default, Eclipse had set in the web.xml the name of the project into display-name. That shall not be copied to the param-value, but the Java package.
Hope this helps.
要解决此问题,请验证您的类MapperIn,需要指定Path:
To resolve this problem, please verify your class MapperIn, there is required to specify Path: