org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException

我用org.glassfish.jersey.servlet.ServletContainer得到了一个ClassNotFoundExceptionexception,但是当我试图用Eclipse Juno启动/重新启动我的Tomcat服务器(v7)时,它特别地在昨晚开始了。

不知道发生了什么事。 奇怪的是,昨天晚上刚刚开始发生的事情,在此之前,它已经完美地工作了。

这里是堆栈跟踪:

 Feb 25, 2014 11:11:19 AM org.apache.catalina.core.ApplicationContext log INFO: Marking servlet com.att.ucomm.admin.UCommAdminFunctions as unavailable Feb 25, 2014 11:11:19 AM org.apache.catalina.core.StandardContext loadOnStartup SEVERE: Servlet /UCommAdminFunctions threw load() exception java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1671) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516) at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:415) at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:397) at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118 ) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1048) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:996) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4762) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5045) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:140) at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3670) at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:424) at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1207) at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1393) at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1403) at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1403) at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1382) at java.lang.Thread.run(Unknown Source) Feb 25, 2014 11:11:19 AM org.apache.catalina.core.StandardContext reload INFO: Reloading Context with name [/UCommAdminFunctions] is completed Feb 25, 2014 11:13:33 AM org.apache.catalina.core.StandardWrapperValve invoke INFO: Servlet com.att.ucomm.admin.UCommAdminFunctions is currently unavailable 

这是我的web.xml

 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>com.att.ucomm.admin.UCommAdminFunctions</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <!-- Register JAX-RS Application, if needed. --> <init-param> <param-name>com.att.ucomm.admin.UCommAdminFunctions</param-name> <param-value>my.package.MyApplication</param-value> </init-param> <!-- Register resources and providers under my.package. --> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.att.ucomm.admin</param-value> </init-param> <!-- Register my custom provider (not needed if it's in my.package) AND LoggingFilter. --> <init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value>com.att.ucomm.admin.SecurityRequestFilter;org.glassfish.jersey.filter.LoggingFilter</param-value> </init-param> <!-- Enable Tracing support. --> <init-param> <param-name>jersey.config.server.tracing</param-name> <param-value>ALL</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>com.att.ucomm.admin.UCommAdminFunctions</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app> 

我确信泽西岛的jar文件仍然存在于WEB-INF / lib中:

WEB-INF / lib与泽西瓶子

问题:

 java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer 

表示您尝试使用Jersey 2.x servlet,但是您正在提供Jersey 1.x库。

对于Jersey 1.x,你必须这样做:

 <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>sample.hello.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> 

有关更多信息,请查看Jersey 1.x文档 。

如果您不想使用Jersey 2.x,那么您将不得不提供Jersey 2.x库。 在基于Maven的项目中,您可以使用以下内容:

 <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>2.xx</version> </dependency> <!-- if you are using Jersey client specific features without the server side --> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.xx</version> </dependency> 

对于Jersey 2.x,你不需要在你的web.xml设置任何东西,只需要提供类似下面的类:

 import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("rest") public class ApplicationConfig extends Application { } 

有关更多信息,请查看Jersey文档 。

也可以看看:

  • java.lang.ClassNotFoundException:com.sun.jersey.spi.container.servlet.ServletContainer
  • Jersey服务与Tomcat和Eclipse
  • 启动tomcat时发生ClassNotFoundException

这是一个日食设置问题,而不是泽西岛问题。

从这个线程ClassNotFoundException:org.glassfish.jersey.servlet.ServletContainer

右键单击你的eclipse项目Properties – > Deployment Assembly – > Add – > Java Build Path Entries – > Gradle Dependencies – > Finish。

所以Eclipse在Apache启动时并没有使用Gradle依赖。

如果您使用的是Jersey 2.x,请使用以下依赖项:

 <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <version>2.XX</version> </dependency> 

XX可以是任何你想要的特定版本。 泽西容器 。

下面的代码在web.xml文件中为我工作

 <servlet> <servlet-name>WebService</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.example.demo.webservice</param-value> //Package </init-param> <init-param> <param-name>unit:WidgetPU</param-name> <param-value>persistence/widget</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>WebService</servlet-name> <url-pattern>/webservices/*</url-pattern> </servlet-mapping> 

假设您使用Jersey 2.25.1,这对我有用 – 我使用Apache Tomcat Web容器:

  <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-server</artifactId> <version>2.25.1</version> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>2.25.1</version> </dependency> 

注意:用您使用的版本replace版本