Java Jersey Rest 上的“com.sun.jersey.api.container.ContainerException:ResourceConfig 实例不包含任何根资源类”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27973924/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:23:33  来源:igfitidea点击:

"com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes" error on Jersey Rest

javaxmlrest

提问by Chris Graves

I know this questions been before on this site but I have taken advice from these pages and haven't been able resolve the problem yet. When trying to run a Rest Web Service using Jersey, I come across the following error message:-

我知道这个问题以前在这个网站上出现过,但我已经从这些页面中得到了建议,但还没有解决这个问题。尝试使用 Jersey 运行 Rest Web 服务时,我遇到以下错误消息:-

EXCEPTION

例外

javax.servlet.ServletException: Servlet.init() for servlet Jersey REST Service threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    java.lang.Thread.run(Thread.java:724)

ROOT CAUSE

根本原因

com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
    com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:99)
    com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1359)
    com.sun.jersey.server.impl.application.WebApplicationImpl.access0(WebApplicationImpl.java:180)
    com.sun.jersey.server.impl.application.WebApplicationImpl.f(WebApplicationImpl.java:799)
    com.sun.jersey.server.impl.application.WebApplicationImpl.f(WebApplicationImpl.java:795)
    com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:795)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:790)
    com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:491)
    com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:321)
    com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605)
    com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:376)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:559)
    javax.servlet.GenericServlet.init(GenericServlet.java:160)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    java.lang.Thread.run(Thread.java:724)

After doing the reasearch, i have narowed it down to 3 problems:-

在进行了研究之后,我将其缩小为 3 个问题:-

  1. Errors within the web.xml.
  2. No resources used within packaged project.
  3. Problems with .jar files.
  1. web.xml 中的错误。
  2. 打包项目中未使用任何资源。
  3. .jar 文件的问题。

I have edited the web.xml as much as possible and have gotten nowhere with it. Below is the current web.xml file i am using.

我已经尽可能多地编辑了 web.xml 并且一无所获。下面是我正在使用的当前 web.xml 文件。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>com.epware.jersey.book</display-name>
  <servlet>
    <servlet-name>Jersey REST Service</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.epware.jersey.book.resources</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

I cannot locate any problems within the linked package. This is probably where the error is but i am struggling to find it myself. Again, the code for this is below.

我在链接的包中找不到任何问题。这可能是错误所在,但我自己正在努力找到它。同样,代码如下。

// Will map the resource to the URL todos
@Path("/books")
public class BooksResource {

    @Context
    UriInfo uriInfo;
    @Context
    Request request;


    @GET
    @Produces(MediaType.TEXT_XML)
    public List<Book> getBooksBrowser() {
        List<Book> books = new ArrayList<Book>();
        books.addAll( BookDao.instance.getModel().values() );
        return books; 
    }

    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public List<Book> getBooks() {
        List<Book> books = new ArrayList<Book>();
        books.addAll( BookDao.instance.getModel().values() );
        return books; 
    }

    @GET
    @Path("count")
    @Produces(MediaType.TEXT_PLAIN)
    public String getCount() {
        int count = BookDao.instance.getModel().size();
        return String.valueOf(count);
    }

    @POST
    @Produces(MediaType.TEXT_HTML)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void newBook(
            @FormParam("id") String id,
            @FormParam("title") String title,
            @FormParam("description") String description,
            @FormParam("authorFirst") String authorFirst,
            @FormParam("authorSecond") String authorSecond,
            @FormParam("publisher") String publisher,
            @FormParam("publishDate") String publishDate,
            @Context HttpServletResponse servletResponse
    ) throws IOException {
        Book book = new Book(id,title,description,authorFirst,authorSecond,publisher,publishDate);
        BookDao.instance.getModel().put(id, book);
        System.out.println(BookDao.instance.getModel().toString());
        servletResponse.sendRedirect("../create_todo.html");
    }

    @Path("{book}")
    public BookResource getBook(
            @PathParam("book") String id) {
        return new BookResource(uriInfo, request, id);
    }

}

I am currently using Jersey jars version 1.18 as shown in the image below:-

我目前使用的是 Jersey jars 1.18 版,如下图所示:-

External Link to Image : http://i.stack.imgur.com/j6tpC.png

图片的外部链接:http: //i.stack.imgur.com/j6tpC.png

Any help is greatly appreciated a i am on limited time now to get this done.

非常感谢任何帮助,我现在在有限的时间内完成这项工作。

采纳答案by Bruno César

As said in comments, your configuration is OK. However, apparently you changed the resource package.

正如评论中所说,您的配置没问题。但是,显然您更改了资源包。

Thus the classpath has not been updated. As in comments, a full clean and rebuild will update the classpath (compiled and resources) and worked.

因此类路径尚未更新。与评论一样,完全清理和重建将更新类路径(已编译和资源)并工作。

回答by Atul

try this @Path(books).It Worked for me or should check

试试这个@Path(books)。它对我有用,或者应该检查一下

com.sun.jersey.config.property.packages

param value in web.xml. Your package is name is correct or not.

web.xml 中的参数值。您的包裹名称是否正确。

回答by Gene

I have no clue why this worked for me, but I had like 6 Jersey sample applications in one workspace and it was giving me the error in one of the applications. After moving that application to another Eclipse workspace and opened a new Eclipse window, the application worked.

我不知道为什么这对我有用,但是我在一个工作区中有 6 个 Jersey 示例应用程序,它在其中一个应用程序中给了我错误。将该应用程序移动到另一个 Eclipse 工作区并打开一个新的 Eclipse 窗口后,该应用程序就可以工作了。