Jersey2 样本休息服务失败

Jersey2 sample rest service failing(Jersey2 样本休息服务失败)

本文介绍了Jersey2 样本休息服务失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to build a sample REST service using Jersey2:

这是我的 POM 依赖项:

Here are my POM dependencies:

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

And here is my 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 实现:

Here is my ResourceConfig implementation:

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:

On deploying it to Tomcat, I tried to access the following 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)

无法理解我错过了什么.有人能指出来吗?

Not able to understand what I am missing. Can some one please point out?

推荐答案

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

All Jersey dependencies should use the same version, i. e. for org.glassfish.jersey.core it should be 2.6 instead of 2.4.1.

这篇关于Jersey2 样本休息服务失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Jersey2 样本休息服务失败

基础教程推荐