简体   繁体   中英

restful web-service - I get java.lang.IncompatibleClassChangeError

Hi I am new to web service I have googled for this many times. Could not get correct solution. This error occurs when I typed the path for my class. any body pls help

exception:

javax.servlet.ServletException: Servlet execution threw an exception

root cause:

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.

My Hello Program is this :

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> ";
  }

} 

and my web.xml file is this.

<?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>

pls anybody help.

I think your version of Jersey is too old. Please check your dependecies.

Jersey 2.0 supports JAX-RS 2.0.

Here is link for new Jersey.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM