No Idea why : The ResourceConfig instance does not

2020-02-10 03:35发布

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.

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

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.access$700(WebApplicationImpl.java:168)
    com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:774)
    com.sun.jersey.server.impl.application.WebApplicationImpl$13.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 :

<?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

标签: java rest jersey
9条回答
狗以群分
2楼-- · 2020-02-10 03:41

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

Example:

ResourceConfig rc = new PackagesResourceConfig("com.package");
HttpServerFactory.create(getBaseURI(), rc);
查看更多
\"骚年 ilove
3楼-- · 2020-02-10 03:42

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

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

You need to register all api class in service.

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);)

    *<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!*****");
    }   
}
查看更多
Luminary・发光体
4楼-- · 2020-02-10 03:43

The error:

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

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

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/classes folder actually contains your class with the proper folder path for the package.

Do a full clean and rebuild then try again.

查看更多
祖国的老花朵
5楼-- · 2020-02-10 03:46

I just ran into this problem using Grizzly with jersey.

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.

    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);
查看更多
forever°为你锁心
6楼-- · 2020-02-10 03:47

I do not no the actual problem you are facing. You can dwonload an example https://github.com/kdmalviyan/RestWithJerseyExample.git you 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

You will get Output like: 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.

查看更多
在下西门庆
7楼-- · 2020-02-10 03:48

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

  <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>
查看更多
登录 后发表回答