Jersey2样本休息服务失败

时间:2014-03-08 05:49:48

标签: java rest maven jersey

我正在尝试使用Jersey2构建示例REST服务:

以下是我的POM依赖项:

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.4.1</version>
    </dependency>
          <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.bundles.repackaged</groupId>
      <artifactId>jersey-guava</artifactId>
      <version>2.6</version>
    </dependency>
  </dependencies>

这是我的web.xml:

<web-app>
  <display-name>jerseysample</display-name>
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.adaequare.rest.config.JerseyResourceInitializer</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>

这是我的ResourceConfig实现:

package com.adaequare.rest.config;

import org.glassfish.jersey.server.ResourceConfig;

public class JerseyResourceInitializer extends ResourceConfig {
    public JerseyResourceInitializer() {
        packages(true, "com.adaequare.resource");
    }
}

最后这是我的示例课程:

package com.adaequare.resource;

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

@Path("/hello")
public class Hello {
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHello(){
        return "<html><title>Hello Jersey</title><body><h1>Hello Jersey</h1></body></html>";
    }

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

}

在将其部署到Tomcat时,我尝试访问以下URL:

http://localhost:8080/jerseysample/rest/hello

以下是我得到的例外情况:

javax.servlet.ServletException: Servlet.init() for servlet Jersey REST Service threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    java.lang.Thread.run(Thread.java:619)
root cause

java.lang.NoSuchFieldError: INCLUDE_ALL
    org.glassfish.jersey.server.ResourceConfig$State.<init>(ResourceConfig.java:110)
    org.glassfish.jersey.server.ResourceConfig.<init>(ResourceConfig.java:351)
    org.glassfish.jersey.server.ResourceConfig$WrappingResourceConfig.<init>(ResourceConfig.java:1016)
    org.glassfish.jersey.server.ResourceConfig.forApplicationClass(ResourceConfig.java:325)
    org.glassfish.jersey.servlet.WebComponent.createResourceConfig(WebComponent.java:459)
    org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:302)
    org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:168)
    org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:358)
    javax.servlet.GenericServlet.init(GenericServlet.java:160)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    java.lang.Thread.run(Thread.java:619)

无法理解我所缺少的内容。有人可以指出吗?

1 个答案:

答案 0 :(得分:7)

所有Jersey依赖项都应使用相同的版本,i。即对于org.glassfish.jersey.core,它应该是2.6而不是2.4.1