如何在Tomcat上使用Jersey-client从另一个资源对JAX-RS资源执行HTTP请求

时间:2015-07-30 18:22:30

标签: java tomcat jersey jax-rs jersey-client

我正在寻找一种方法,如何在应用程序#2的资源中调用应用程序#1的GET资源。因此我尝试使用Jersey-client API。

两个应用程序都在Apache Tomcat / 8.0.23(localhost:8099)上运行。对于这两者,我使用Jersey 2.19作为JAX-RS实现来创建RESTful资源。

应用程序#1(记录应用程序):

@Path("records")
public class RecordResource {

  @GET
  @Path("test")
  @Produces(MediaType.TEXT_PLAIN)
  public String getTest() {
    return "Hello World :-)";
  }
}

申请#2(claim-app):

@Path("claims")
public class ClaimResource {

  @POST
  public void postTest() {
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://localhost:8099/record-app/rest/records/test");

    Invocation.Builder invocationBuilder = target.request(MediaType.TEXT_PLAIN);
    Response responseRecordApp = invocationBuilder.get();

    System.out.println(responseRecordApp.getStatus());
    System.out.println(responseRecordApp.readEntity(String.class));
}

在声明资源上发布POST后,我得到了这个NoClassDefFoundError:ThreadPoolExecutorProvider

30-Jul-2015 09:56:21.476 SEVERE [http-apr-8099-exec-7] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [org.claims.App] in context with path [/claim-app] threw exception [org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: org/glassfish/jersey/spi/ThreadPoolExecutorProvider] with root cause
 java.lang.ClassNotFoundException: org.glassfish.jersey.spi.ThreadPoolExecutorProvider
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
...

对我而言,这看起来像Tomcat缺少一些库。我想知道这是怎么回事,因为我正在使用Maven依赖(并且我没有将它们标记为提供)。

claim-app的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-resource"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>claim-app</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <outputDirectory>C:\apache-tomcat-8.0.23\webapps\</outputDirectory>
                </configuration>
            </plugin>
        </plugins>
        <finalName>${artifactId}</finalName>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>2.17</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.19</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>2.19</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>
</project>

无论如何,我将缺少的库(ThreadPoolExecutorProvider所在的位置)添加到tomcat / lib文件夹中。之后我得到了更多的NoClassDefFoundErrors,所以我最终得到了这个罐子列表:

jersey-apache-connector-2.19
jaxrs-ri-2.19
jersey-container-jdk-http-2.19
jersey-container-simple-http-2.19
jersey-guava-2.19
jersey-server-2.19
jersey-common-2.19
jersey-media-jaxb-2.19
jersey-container-servlet-core-2.19
hk2-api-2.4.0-b25
hk2-locator-2.4.0-b25
hk2-utils-2.4.0-b25
javax.inject-2.4.0-b25
jersey-client-2.19
jersey-container-servlet-2.19

在我摆脱NoClassDefFoundErrors后,发生以下异常:

type Exception report

message org.glassfish.jersey.server.ContainerException: java.lang.LinkageError: loader constraint violation: loader (instance of java/net/URLClassLoader) previously initiated loading for a different type with name "org/glassfish/hk2/api/ServiceLocator"

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.lang.LinkageError: loader constraint violation: loader (instance of java/net/URLClassLoader) previously initiated loading for a different type with name "org/glassfish/hk2/api/ServiceLocator"
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:421)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:222)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

org.glassfish.jersey.server.ContainerException: java.lang.LinkageError: loader constraint violation: loader (instance of java/net/URLClassLoader) previously initiated loading for a different type with name "org/glassfish/hk2/api/ServiceLocator"
    org.glassfish.jersey.servlet.internal.ResponseWriter.rethrow(ResponseWriter.java:256)
    org.glassfish.jersey.servlet.internal.ResponseWriter.failure(ResponseWriter.java:238)
    org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:483)
    org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:313)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:288)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1110)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:401)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:222)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.LinkageError: loader constraint violation: loader (instance of java/net/URLClassLoader) previously initiated loading for a different type with name "org/glassfish/hk2/api/ServiceLocator"
    java.lang.ClassLoader.defineClass1(Native Method)
    java.lang.ClassLoader.defineClass(Unknown Source)
    java.security.SecureClassLoader.defineClass(Unknown Source)
    java.net.URLClassLoader.defineClass(Unknown Source)
    java.net.URLClassLoader.access$100(Unknown Source)
    java.net.URLClassLoader$1.run(Unknown Source)
    java.net.URLClassLoader$1.run(Unknown Source)
    java.security.AccessController.doPrivileged(Native Method)
    java.net.URLClassLoader.findClass(Unknown Source)
    java.lang.ClassLoader.loadClass(Unknown Source)
    java.lang.ClassLoader.loadClass(Unknown Source)
    org.glassfish.jersey.internal.inject.Providers.getServiceHandles(Providers.java:354)
    org.glassfish.jersey.internal.inject.Providers.getCustomProviders(Providers.java:201)
    org.glassfish.jersey.process.internal.ExecutorProviders.getQualifierToProviderMap(ExecutorProviders.java:191)
    org.glassfish.jersey.process.internal.ExecutorProviders.createInjectionBindings(ExecutorProviders.java:103)
    org.glassfish.jersey.client.ClientConfig$State.initRuntime(ClientConfig.java:415)
    org.glassfish.jersey.client.ClientConfig$State.access$000(ClientConfig.java:88)
    org.glassfish.jersey.client.ClientConfig$State$3.get(ClientConfig.java:120)
    org.glassfish.jersey.client.ClientConfig$State$3.get(ClientConfig.java:117)
    org.glassfish.jersey.internal.util.collection.Values$LazyValueImpl.get(Values.java:340)
    org.glassfish.jersey.client.ClientConfig.getRuntime(ClientConfig.java:726)
    org.glassfish.jersey.client.ClientRequest.getConfiguration(ClientRequest.java:285)
    org.glassfish.jersey.client.JerseyInvocation.validateHttpMethodAndEntity(JerseyInvocation.java:126)
    org.glassfish.jersey.client.JerseyInvocation.<init>(JerseyInvocation.java:98)
    org.glassfish.jersey.client.JerseyInvocation.<init>(JerseyInvocation.java:91)
    org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:402)
    org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:302)
    org.claims.controller.ClaimResource.postTest(ClaimResource.java:39)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:164)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:181)
    org.glassfish.jersey.server.model.internal.VoidVoidDispatcherProvider$VoidToVoidDispatcher.doDispatch(VoidVoidDispatcherProvider.java:80)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:101)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
    org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:305)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:288)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1110)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:401)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:222)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.23 logs.

Apache Tomcat/8.0.23

如果我在一个没有在tomcat上运行的简单Java类中使用相同的postTest()代码,我不会得到异常并且结果是正确的:

 200
 Hello World :-) 

所以我认为问题必须在Tomcat服务器的某个地方。对我来说,看起来我在应用程序#2中使用的Jersey客户端以某种方式逃脱了servlet容器。这就是为什么我可能得到了所有这些NoClassDefErrors。但我不确定......

有人知道这里出了什么问题吗? 或者是否有另一种方法可以从另一个REST资源在REST资源上发出HTTP请求?

解: 解决方案是删除像@peeskillet建议的jersey-client依赖。

0 个答案:

没有答案