Java 不知道为什么:ResourceConfig 实例不包含任何根资源类

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

No Idea why : The ResourceConfig instance does not contain any root resource classes

javarestjersey

提问by Vahid Haratian

I'm new to jersey and web services and I'm try to run a simple RESTful web service. I followed http://www.mkyong.com/webservices/jax-rs/jersey-hello-world-example/but my project doesn't use maven and I download the jersey.1.17.1.jar and include it to my project path.

我是 jersey 和 web 服务的新手,我正在尝试运行一个简单的 RESTful web 服务。我关注了http://www.mkyong.com/webservices/jax-rs/jersey-hello-world-example/但我的项目不使用 maven,我下载了 jersey.1.17.1.jar 并将其包含到我的项目路径。

When I want to call the service on http://localhost:8080/sycotext/rest/service/SOMETEXTI get this error :

当我想调用该服务时,出现http://localhost:8080/sycotext/rest/service/SOMETEXT此错误:

HTTP Status 500 - Servlet.init() for servlet sycoText-servlet threw exception

this is the stack trace :

这是堆栈跟踪:

javax.servlet.ServletException: Servlet.init() for servlet sycoText-servlet threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:76)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:934)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:515)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1010)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:640)
    org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1618)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1576)
    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:1331)
    com.sun.jersey.server.impl.application.WebApplicationImpl.access0(WebApplicationImpl.java:168)
    com.sun.jersey.server.impl.application.WebApplicationImpl.f(WebApplicationImpl.java:774)
    com.sun.jersey.server.impl.application.WebApplicationImpl.f(WebApplicationImpl.java:770)
    com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:770)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:765)
    com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:489)
    com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:319)
    com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605)
    com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:374)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:557)
    javax.servlet.GenericServlet.init(GenericServlet.java:160)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:76)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:934)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:515)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1010)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:640)
    org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1618)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1576)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    java.lang.Thread.run(Thread.java:724)

here is my code :

这是我的代码:

package ir.sycotech.text.server.service;

import javax.ws.rs.*;
import javax.ws.rs.core.Response;

@Path("/service")
public class SycoTextService {
    @GET
    @Path("/{param}")
    public Response getMsg(@PathParam("param") String msg) {
        String output = "Jersey say : " + msg;
        return Response.status(200).entity(output).build();
   }

and here is my web.xml :

这是我的 web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Restful Web Application</display-name>


    <servlet>
        <servlet-name>sycoText-servlet</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>ir.sycotech.text.server.service</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>sycoText-servlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>

I have specified my packagename correctly in the web.xml file and I don't know why I got this error, I will be really appreciate if anyone knows what is the problem

我已在 web.xml 文件中正确指定了我的包名,但我不知道为什么会出现此错误,如果有人知道问题出在哪里,我将不胜感激

采纳答案by Bogdan

The error:

错误:

com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.

com.sun.jersey.api.container.ContainerException:ResourceConfig 实例不包含任何根资源类。

means that Jersey can't find service classes. That can be caused by a wrongly named package for the com.sun.jersey.config.property.packagesparameter or if the package name is correct but it does not contain resource classes (people sometimes forget to add the @Pathannotation on the class).

意味着 Jersey 找不到服务类。这可能是由于com.sun.jersey.config.property.packages参数的包命名错误或包名称正确但不包含资源类(人们有时会忘记@Path在类上添加注释)引起的。

But I can't find anything wrong with your setup. So this should work!

但是我找不到你的设置有什么问题。所以这应该有效!

Check that your application deployed correctly and that your WEB-INF/classesfolder actually contains your class with the proper folder path for the package.

检查您的应用程序是否正确部署,并且您的WEB-INF/classes文件夹实际上包含您的类以及包的正确文件夹路径。

Do a full clean and rebuild then try again.

进行全面清理并重建,然后重试。

回答by Kuldeep Singh

I do not no the actual problem you are facing. You can dwonload an example https://github.com/kdmalviyan/RestWithJerseyExample.gityou have to take following actions after downloading: 1. mvn clean install 2. deploy war to your server 3. access "JerseyExample-0.0.1-SNAPSHOT/rest/hello/hello Kuldeep Singh" on your server

我不知道你面临的实际问题。您可以下载一个示例https://github.com/kdmalviyan/RestWithJerseyExample.git下载后您必须执行以下操作: 1. mvn clean install 2. 将 war 部署到您的服务器 3. 访问“JerseyExample-0.0.1-SNAPSHOT /rest/hello/hello Kuldeep Singh" 在您的服务器上

You will get Output like: Jersey say : hello Kuldeep Singh

你会得到这样的输出: Jersey say : hello Kuldeep Singh

I suggest you to follow exact steps without any change anything first. If you get correct result then you can modify according to your need. Please make sure if you are renaming package, rename package in web.xml too.

我建议您先按照确切的步骤进行操作,而无需进行任何更改。如果得到正确的结果,则可以根据需要进行修改。请确保如果您要重命名包,也要在 web.xml 中重命名包。

回答by Ben Glasser

I just ran into this problem using Grizzly with jersey.

我刚刚在使用 Grizzly 和球衣时遇到了这个问题。

When you fire up a a Grizzly container you have to pass in a map telling Grizzly where to find your resources. If you created your project from an archetype like I did or just moved some things around you also have to update this value which is easy to overlook.

当你启动一个 Grizzly 容器时,你必须传入一张地图,告诉 Grizzly 在哪里可以找到你的资源。如果您像我一样从原型创建项目,或者只是在您周围移动了一些东西,则还必须更新此值,这很容易被忽视。

    final static String YOUR_PACKAGE_NAME_GOES_HERE = "where.ever.your.resource.package.happens.to.be"

    final Map<String, String> initParams = new HashMap<String, String>();

    initParams.put("com.sun.jersey.config.property.packages",
            YOUR_PACKAGE_NAME_GOES_HERE);

    System.out.println("Starting grizzly...");
    SelectorThread threadSelector = GrizzlyWebContainerFactory.create(BASE_URI, initParams);

回答by user3621633

I ran into this issue, as well. There were two things that I did to get it to work, with the first being unrelated to this problem.

我也遇到了这个问题。为了让它工作,我做了两件事,第一件事与这个问题无关。

First, I'm using jersey 1.19. However, I didn't realize there was a file named javax.ws.rs-api-2.0.jarin the WEB-INF/libon the server. This file was added two years ago and, IIRC, works in conjunction with jersey 2.x but not 1.x. I effectively removed the file (renamed it with .bakas its extension).

首先,我使用球衣 1.19。但是,我不知道有一个叫文件 javax.ws.rs-api-2.0.jarWEB-INF/lib服务器上。这个文件是两年前添加的,IIRC 与 jersey 2.x 一起工作,但不是 1.x。我有效地删除了该文件(将其重命名为.bak作为其扩展名)。

Second, I created a JAR file in Eclipse for the web service. I did not use Maven or Ant or anything like that. Just a simple export. Now, the error can mean there is nothing in your code that references jersey. But I wrote a simple test class and everything should be working, I thought. Turns out the export wasn't creating the JAR file properly. I opened the JAR and the class was empty. No wonder it was generating this error! I exported once more and instead of checkmarking Export generated class files and resources, I checkmarked Export all output folders for checked projects. I also have multiple packages in this one project so that could be related to why the export wasn't functioning properly. Once I did that, it worked!

其次,我在 Eclipse 中为 Web 服务创建了一个 JAR 文件。我没有使用 Maven 或 Ant 或类似的东西。只是一个简单的导出。现在,该错误可能意味着您的代码中没有任何引用 jersey 的内容。但我写了一个简单的测试类,一切都应该正常工作,我想。结果是导出没有正确创建 JAR 文件。我打开了 JAR,班级是空的。难怪它会产生这个错误!我再次导出,而不是勾选标记Export generated class files and resources,我勾选了Export all output folders for checked projects. 我在这个项目中也有多个包,因此这可能与导出无法正常运行的原因有关。一旦我这样做了,它就奏效了!

I tried the export again but, this time, I checkmarked Export generated class files and resourcesand selected every resource in the project. What was different from last time was I selected .settings. Last time, .settingswasn't selected. Again, this worked, too!

我再次尝试导出,但这次我选中Export generated class files and resources并选择了项目中的每个资源。与上次不同的是我选择了.settings。上次,.settings没有被选中。同样,这也有效!

I hope this helps someone out there with a similar problem. Sometimes, it's just something as stupid as confirming your JAR file is correct. I use java decompilerto inspect the JAR file.

我希望这可以帮助有类似问题的人。有时,这就像确认您的 JAR 文件是正确的一样愚蠢。我使用java 反编译器来检查 JAR 文件。

回答by ct.tan

I encounter similar problem. Please check your initialization process whether you have registered the api class properly.

我遇到了类似的问题。请检查您的初始化过程是否正确注册了 api 类。

In your case, the initialization class is ir.sycotech.text.server.service

在你的情况下,初始化类是 ir.sycotech.text.server.service

You need to register all api class in service.

您需要在服务中注册所有 api 类。

Here is my example:

这是我的例子:

I will hit the error if the following classes are not registered, ( register(CtoFService.class); register(FtoCService.class); register(TriggerCmd.class);)

如果未注册以下类,我将遇到错误,( register(CtoFService.class); register(FtoCService.class); register(TriggerCmd.class);)

    *<init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>my.mimos.hcserver.init.MyApplication</param-value>
    </init-param>*



 @ApplicationPath("/HCRestServer/")
public class MyApplication extends ResourceConfig{
    public MyApplication() {
        System.out.println("******Started!*****");
        register(CtoFService.class);
        register(FtoCService.class);
        register(TriggerCmd.class);
        register(CORSResponseFilter.class);
        System.out.println("******Done registration!*****");
    }   
}

回答by Jeet

Please ensure that your package "com.sun.jersey.config.property.packages" is registered correctly in Web.xml ... Good Luck !!

请确保您的包“com.sun.jersey.config.property.packages”在 Web.xml 中正确注册......祝你好运!

  <servlet>
      <servlet-name>Jersey REST Service</servlet-name>
      <servletclass>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
     <init-param>
     <param-name>com.sun.jersey.config.property.packages</param-name>
     <param-value>com.rest.service</param-value>
     </init-param>
     <init-param>
     <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
     <param-value>true</param-value>
     </init-param>         
     <load-on-startup>1</load-on-startup>
  </servlet>

回答by otaviodecampos

If you trying to use HttpServerFactory, you must pass a PackagesResourceConfig.

如果您尝试使用 HttpServerFactory,则必须传递 PackagesResourceConfig。

Example:

例子:

ResourceConfig rc = new PackagesResourceConfig("com.package");
HttpServerFactory.create(getBaseURI(), rc);

回答by PJ_Finnegan

I had the same problem, which I solved by specifying BOTH the classes package AND removing load-on-startupaltogether, i.e.:

我遇到了同样的问题,我通过指定两个类包并完全删除启动时加载来解决这个问题,即:

<servlet>
    <servlet-name>my-servlet-name</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>my.package.name</param-value>
    </init-param>
    <!-- <load-on-startup>1</load-on-startup> -->
</servlet>

I guess because I had more than one servlet under the same package and Jersey coudn't pick the "root" one to be loaded on startup.

我猜是因为我在同一个包下有多个 servlet,而 Jersey 无法选择要在启动时加载的“根”servlet。

回答by MarAvFe

Well, none of the above answers worked for me. They made me double check every step and package name, though. As Bogdan said, there were no compiled classes inside the target/classes/folder.

好吧,以上答案都不适合我。不过,他们让我仔细检查每一步和包名。正如 Bogdan 所说,target/classes/文件夹中没有已编译的类。

Check that your application deployed correctly and that your WEB-INF/classes folder actually contains your class with the proper folder path for the package.

检查您的应用程序是否正确部署,并且您的 WEB-INF/classes 文件夹实际上包含您的类以及包的正确文件夹路径。

My solution was just cleaning the project and compiling different. Instead of just going for mvn war:warI did:

我的解决方案只是清理项目并编译不同的。而不是只是去mvn war:war我做了:

$ mvn clean:clean $ mvn compile $ mvn war:war

$ mvn clean:clean $ mvn compile $ mvn war:war

Then it compiled fine and the OP error disappeared.

然后它编译得很好,OP错误消失了。