restful web-service - 我得到了java.lang.IncompatibleClassChangeError

时间:2013-12-28 11:55:21

标签: java web-services rest

您好我是网络服务的新手,我已多次使用Google搜索。无法得到正确的解决方案。当我输入类的路径时会发生此错误。任何身体请帮助

例外:

javax.servlet.ServletException: Servlet execution threw an exception

根本原因:

java.lang.IncompatibleClassChangeError: Class javax.ws.rs.core.Response$Status does not implement the requested interface javax.ws.rs.core.Response$StatusType
    com.sun.jersey.spi.container.ContainerResponse.getStatus(ContainerResponse.java:599)
    com.sun.jersey.spi.container.ContainerResponse$CommittingOutputStream.commitWrite(ContainerResponse.java:157)
    com.sun.jersey.spi.container.ContainerResponse$CommittingOutputStream.write(ContainerResponse.java:134)
    sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
    sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source)
    sun.nio.cs.StreamEncoder.implFlush(Unknown Source)
    sun.nio.cs.StreamEncoder.flush(Unknown Source)
    java.io.OutputStreamWriter.flush(Unknown Source)
    java.io.BufferedWriter.flush(Unknown Source)
    com.sun.jersey.core.util.ReaderWriter.writeToAsString(ReaderWriter.java:191)
    com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider.writeToAsString(AbstractMessageReaderWriterProvider.java:128)
    com.sun.jersey.core.impl.provider.entity.StringProvider.writeTo(StringProvider.java:88)
    com.sun.jersey.core.impl.provider.entity.StringProvider.writeTo(StringProvider.java:58)
    com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:302)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1510)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
    com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:540)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:715)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.37 logs.

我的Hello程序是:

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

// Plain old Java Object it does not extend as class or implements 
// an interface

// The class registers its methods for the HTTP GET request using the @GET annotation. 
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML. 

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello
@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>FirstRestWebService</display-name>
 <display-name>FirstRestWebService</display-name>
   <servlet>
        <servlet-name>RestServlet</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>org.raj</param-value>
        </init-param>
    </servlet>
  <servlet-mapping>
    <servlet-name>RestServlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
  </web-app>

请任何人帮忙。

1 个答案:

答案 0 :(得分:0)

我认为你的泽西岛版本太旧了。请检查您的依赖项。

Jersey 2.0 supports JAX-RS 2.0.

Here is link for new Jersey.