【问题标题】:Jersey REST The ResourceConfig instance does not contain any root resource classes [duplicate]Jersey REST ResourceConfig 实例不包含任何根资源类 [重复]
【发布时间】:2011-08-31 22:48:05
【问题描述】:

虽然这是一个古老的问题,但我仍然找不到解决这个问题的答案。 如果您发现我的任何陈述不正确,请更正。

我有一个 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 是一条重要消息吗?

【问题讨论】:

  • 你能分享你解决了什么问题吗?我有同样的问题!即使我已经用细齿梳完成了所有事情,我仍然得到 404
  • @user759646 面临同样的问题,我在 weblogic 中运行它

标签: java jersey jax-rs


【解决方案1】:

我有同样的错误信息,并通过修改我的 web.xml 文件解决了它。确保里面有这个:

<init-param>
   <param-name>com.sun.jersey.config.property.packages</param-name>
   <param-value>your.package.name.here</param-value>
</init-param>

【讨论】:

  • 这对我不起作用......即使在我的 web.xml 中添加了这个,错误仍然存​​在
【解决方案2】:

我找到了 Jersey 无法找到根资源的另一个原因。错误消息是相同的,所以我认为这是一个很好的理由来记录根本原因,以防其他人偶然发现它。

我有一个根资源类,上面有以下注释:

@Singleton
@Path("/helloWorld")
@Service(...) // my own custom annotation
public class MyRootResource {
...
}

这将导致 Jersey 中的根资源扫描失败。

解决方法是更改​​注释的顺序:

@Service(...) // my own custom annotation
@Singleton
@Path("/helloWorld")
public class MyRootResource {
...
}

这行得通。

【讨论】:

  • 为我工作,我使用@RequestMapping(value = "/api"),然后由@Path("/api") 更改
【解决方案3】:

首先,请注意,在一个类上使用@GET 不足以将其标识为根资源类;该类必须有一个@Path。你的有,所以这不是问题。

我遇到了和你一样的问题:它在 Eclipse 中工作,但是当我构建它以供外部使用时却没有工作。

我正在使用嵌入式 Jetty,其 main() 如下所示:

public static void main(String argv[])
{
    try
    {
        // from http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty
        Server server = new Server(8080);
        ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        contextHandler.setContextPath("/");
        server.setHandler(contextHandler);

        // http://stackoverflow.com/questions/9670363/how-do-i-programmatically-configure-jersey-to-use-jackson-for-json-deserializa
        final PackagesResourceConfig prc = new PackagesResourceConfig("com.ultimatefoodfight.server.api");
        final Map<String, Object> prcProperties = prc.getProperties();
        prcProperties.put(JSONConfiguration.FEATURE_POJO_MAPPING, true);

        // from http://stackoverflow.com/questions/7421574/embedded-jetty-with-jersey-or-resteasy
        contextHandler.addServlet(new ServletHolder(new ServletContainer(prc)), "/*");

        server.start();
        server.join();
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
}

(当嵌入 Jetty 时,它代替了 web.xml。) com.sun.jersey.api.core.PackagesResourceConfig 扫描命名类以查找根资源提供程序类——我确定 web.xml 是只是指向同一个。这在 Eclipse 中运行良好。服务器启动在控制台中正确报告了这一点:

INFO: Scanning for root resource and provider classes in the packages:
  com.ultimatefoodfight.server.api
Jul 29, 2012 7:31:28 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class com.ultimatefoodfight.server.api.Users
  class com.ultimatefoodfight.server.api.Contests

但是,当我在服务器上启动应用程序时,我只得到前两行,没有找到根资源类。

事实证明,当你制作一个罐子时,它的构造方式有不同的选择;特别是,如果你只是通过列出类的路径来实现它,你会得到 只是那些条目。但是如果你用通配符将 jar 指向目录,你也会得到所有的中间路径。请参阅此消息以获取提示我的示例:https://groups.google.com/d/msg/neo4j/0dNqGXvEbNg/xaNlRiU1cHMJ

PackagesResourceConfig 类依赖于具有包名的目录,以便在其中找到类。我回到 Eclipse,在“Export ... > Jar”对话框的底部找到了一个“Add directory entries”选项。我打开它,再次导出 jar,然后扫描找到了我的资源类。您需要在构建环境中找到一些类似的选项。

【讨论】:

  • 嘿克里斯!非常感谢您的帖子。我厌倦了球衣,因为有时它用于检测我的根资源,有时却没有。我不知道问题是什么,您的帖子指出了确切的问题并帮助我解决了它。再次感谢!
【解决方案4】:

我也花了 5 到 6 个小时来解决这个问题,终于解决了这个问题。这也许对你也有用。

我一直在使用的源代码位于Lars Vogel's tutorial page。使用 Apache Tomcat 6.0.20、asm-all-3.3.1.jar、jersey-bundle-1.17.1.jar 和 jsr311-api-1.1.1.jar 我得到了与 OP 相同的结果,即

INFO: Root resource classes found:
  class com.mypackage.MyClass
13/03/2013 4:32:30 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.

我在各种部署文件中有以下设置:

context root = rivets
<url-pattern>/rest</url-pattern>
@Path("/hello")

尝试使用以下 URL 访问资源时出现 404 错误

 http://localhost:8080/rivets/rest/hello

最终能够通过将 url 模式更改为来修复它:

<url-pattern>/rest/*</url-pattern>

虽然我不确定当我开始引入更复杂的资源时这将如何维持。无论如何,希望这可以帮助某人。

【讨论】:

    【解决方案5】:

    我发现“找不到提供程序类”消息很吓人但并不重要。

    “ResourceConfig 实例不包含任何根资源类”是一个更大的问题。

    我发现资源类需要用@Path注解,而provider类(如果你有的话——我们用一个来映射异常到HTTP响应代码)需要用@Provider注解,两者都需要放在正确的包装中,然后泽西岛会找到它们。

    【讨论】:

      【解决方案6】:

      您可能需要使用这样的网址

      http://localhost:8080/<web_context_root>/nd/hello
      

      【讨论】:

      • 或者我们可以更改 server.xml 文件中的 Web_Context_Root。
      【解决方案7】:

      所以我阅读了答案,但仍然遇到同样的问题。我是这样解决的。

      在“WEB-INF”文件夹中添加了“jetty-web.xml”。因此: src/main/webapp/WEB-INF/jetty-web.xml

      jetty-web.xml的内容是:

      <?xml version="1.0"  encoding="ISO-8859-1"?>
      <!DOCTYPE Configure PUBLIC "-" "http://www.eclipse.org/jetty/configure.dtd">
      <Configure class="org.eclipse.jetty.webapp.WebAppContext">
          <Set name="contextPath">/YourEndpoint</Set>
      </Configure>
      

      我的休息现在可以在:

      http://localhost:8080/YourEndpoint/webresources/someMethod
      

      希望这对那里的人有所帮助。

      【讨论】:

      • 这是因为我忘记在 web.xml 中定义端点
      • resteasy.servlet.mapping.prefix/rest
      【解决方案8】:

      我遇到了同样的问题。通过在 web.xml 中修复 init-param 标记下的参数值来解决它。 默认情况下,Eclipse 已在 web.xml 中将项目名称设置为 display-name。那不应该复制到param-value,而是Java包。

        <init-param>
          <param-name>com.sun.jersey.config.property.packages</param-name>
          <param-value>com.test.demo</param-value>
          </init-param> 
      

      希望这会有所帮助。

      【讨论】:

        【解决方案9】:

        要解决这个问题,请验证你的类MapperIn,需要指定Path:

        import javax.ws.rs.GET;
        import javax.ws.rs.OPTIONS;
        import javax.ws.rs.Path;
        import javax.ws.rs.PathParam;
        import javax.ws.rs.Produces;
        import javax.ws.rs.core.MediaType;
        
        import com.lot.account.api.doc.GetMemberAccountsIn;
        import com.lot.common.doc.CommonApi;
        import com.lot.common.doc.FlowData;
        import com.lot.security.authorization.RequestData;
        
        
        @Path("/account/member/")
        public class MapperIn {
        
            @GET
            @Produces(MediaType.APPLICATION_JSON)
            @Path("/{memberId}")
            public GetAccountIn prepareGetAccount(@PathParam("memberId") String memberId) {
        
                // create apiInput
                GetAccountIn apiInput = new GetAccountIn ();
        
                // map values from url
                    apiInput.setMemberId(memberId);
        
        
                return apiInput;
            }
        //...
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多