7

我刚刚基于这个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

4 回答 4

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]

于 2014-06-05T11:02:35.493 回答
2

你得到

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

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

因此,将jersey-servlet-1.17.jar添加到您的项目中。

于 2014-06-10T14:14:50.183 回答
1

由于构建路径问题,我们收到此错误。您应该在构建路径中添加“服务器运行时”库。

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

请按照以下步骤解决找不到类的异常。

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

谢谢, 萨钦 GN

于 2015-08-07T23:03:49.807 回答
0

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

泽西库

于 2015-01-24T10:22:52.630 回答