在使用jersey框架搭建服务时,经常会报出如下异常:
Java代码
com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes. at com.sun.jersey.server.impl.application.RootResourceUriRules.
(RootResourceUriRules.java:99) at com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1331) at com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:168) at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:774) at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:770) at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193) at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:770) at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:765) at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:489) at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:319) at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605) at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210) at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:374) at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:557) at javax.servlet.GenericServlet.init(GenericServlet.java:212) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1206) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1026) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4421) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4734) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601) at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1079) at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1002) at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:506) at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317) at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065) at org.apache.catalina.core.StandardHost.start(StandardHost.java:840) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057) at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) at org.apache.catalina.core.StandardService.start(StandardService.java:525) at org.apache.catalina.core.StandardServer.start(StandardServer.java:754) at org.apache.catalina.startup.Catalina.start(Catalina.java:595) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
下面是出现这种异常的几种情况
1、在不与spring整合,而是直接使用jersey搭建服务时,如果只在web.xml 中配置了如下代码:
abc com.sun.jersey.spi.container.servlet.ServletContainer 1 abc /services/*
此时启动tomcat,会报出上面的异常,原因是实例张不包含任何启动资源的类,你要确保实例已经创建,例如:
Java代码
@Path("abc") @Produces("application/xml") @Consumes("application/xml") public class Abc { @POST @Produces("application/xml") @Consumes("application/xml") public void abc(){ } }
这样再启动,就可以了。
2、在与spring整合中时,如果出现如上异常,原因可能是:
1)缺少spring-jersey.jar包
将spring-jersey.jar包添加到工程lib下,然后修改下web.xml配置文件
abc com.sun.jersey.spi.spring.container.servlet.SpringServlet 1 abc /services/*
然后再重新启动tomcat,就可以了。
2)如果按照1)操作完成后,tomcat启动仍然报上面的异常,此时需要在web.xml中在重新添加点配置信息(红色部分):
abc com.sun.jersey.spi.spring.container.servlet.SpringServlet com.sun.jersey.config.property.packages com.xxyd.map.webservice 1 abc /services/*
此时再启动tomcat就可以了。
当然,还有一种方式就是重新将“com.sun.jersey.spi.spring.container.servlet.SpringServlet”替换回“com.sun.jersey.spi.spring.container.servlet.SpringServlet”,但如果是这样的话,就失去了spring与jersey整合的意义了,不提倡如此解决。
2、冲突问题
java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/
导入jersey jar 包就不要再导入javax.ws.rs,会引发冲突