异常java.lang.ClassNotFoundException:com.sun.jersey.spi.container.servlet.ServletContainer

时间:2013-10-23 17:55:41

标签: rest tomcat jersey

我刚刚根据http://www.vogella.com/articles/REST/article.html制作了我的第一个REST api 我正在使用eclipse kepler,tomcat7和Jersey

当我尝试“在服务器上运行”时出现此错误:

SEVERE: Servlet /de.vogella.jersey.first threw load() exception
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:527)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:509)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:137)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

以下是源代码:

package de.vogella.jersey.first;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class Hello {

  // This method is called if TEXT_PLAIN is request
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello Jersey";
  }

  // This method is called if XML is request
  @GET
  @Produces(MediaType.TEXT_XML)
  public String sayXMLHello() {
    return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
  }

  // This method is called if HTML is request
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHtmlHello() {
    return "<html> " + "<title>" + "Hello Jersey" + "</title>"
        + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
  }

} 

这是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>de.vogella.jersey.first</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>de.vogella.jersey.first</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> 

这是我从泽西岛添加的库:

asm-all-repackaged-2.2.0-b14.jar
cglib-2.2.0-b14.jar
guava-14.0.1.jar
hk2-api-2.2.0-b14.jar
hk2-locator-2.2.0-b14.jar
hk2-utils-2.2.0-b14.jar
javax.annotation-api-1.2.jar
javax.inject-2.2.0-b14.jar
javax.servlet-api-3.0.1.jar
javax.ws.rs-api-2.0.jar
jaxb-api-2.2.7.jar
jersey-client.jar
jersey-common.jar
jersey-container-servlet-core.jar
jersey-container-servlet.jar
jersey-server.jar
org.osgi.core-4.2.0.jar
osgi-resource-locator-1.0.1.jar
persistence-api-1.0.jar
validation-api-1.1.0.Final.jar

4 个答案:

答案 0 :(得分:6)

如果您使用的是jersey 2.x API,则需要重命名这些类,如下所示,您的问题将得到解决。

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>org.glassfish.jersey.config.property.packages</param-name>
      <param-value>com.practise.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

Karthik [又名DjNickZ]

答案 1 :(得分:2)

你要

com.sun.jersey.spi.container.servlet.ServletContainer

这意味着您使用的是不同版本的jar。您已添加jersey-server.jarjar升级为jersey-servlet-1.17.jar

所以将jersey-servlet-1.17.jar添加到您的项目中。

答案 2 :(得分:1)

由于构建路径问题,我们收到此错误。您应该在Build Path中添加“Server Runtime”库。

“java.lang.ClassNotFoundException:com.sun.jersey.spi.container.servlet.ServletContainer”

请按照以下步骤解决未找到课程的异常。

右键点击项目 - &gt;构建路径 - &gt; Java构建路径 - &gt;添加库 - &gt;服务器运行时 - &gt; Apache Tomcat v7.0

谢谢, Sachin G N

答案 3 :(得分:0)

您需要将以下jar文件添加到您的web-inf / lib文件夹中,您还需要配置构建路径 - 右键单击​​您的项目 - 构建路径 - 配置构建路径 - 库 - 添加外部jar (添加以下所有jar文件)。并重启服务器

Jersey libs