流易

日日行,不怕千万里;常常做,不怕千万事。(https://github.com/poazy)

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一、问题描述

1)项目一开始采用JAX-RS 2.1+Jersey 2.26调用REST服务能正常调用并获得正确响应;

2)当项目引入dubbo 2.6.2后也用到rest而dubbo要用到RESTEasy,项目就引入了RESTEasy;

3)项目引入RESTEasy后,JAX-RS 2.1+Jersey 2.26调用REST服务出现异常!异常信息如下:

javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request
    at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:321)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:439)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:61)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.post(ClientInvocationBuilder.java:219)
    at com.lutao.pigeon.iclient.ibeplus.common.IBEPlusCaller.invoke(IBEPlusCaller.java:63)
    at com.lutao.pigeon.gds.ibe.service.impl.IbeGdsShoppingService.flightShopping(IbeGdsShoppingService.java:89)
    at com.lutao.pigeon.gds.resource.impl.GdsShoppingResource.flightShopping(GdsShoppingResource.java:37)
    at com.lutao.pigeon.gds.resource.impl.GdsShoppingResource$$FastClassBySpringCGLIB$$77b131d0.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
    at com.lutao.openservice.common.aspect.BaseRestAspectProcessor.syncAroundHandle(BaseRestAspectProcessor.java:340)
    at com.lutao.openservice.common.aspect.BaseRestAspectProcessor.lambda$asyncAroundHandle$0(BaseRestAspectProcessor.java:267)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javax.ws.rs.ProcessingException: RESTEASY003215: could not find writer for content-type text/html type: java.lang.String
    at org.jboss.resteasy.core.interception.ClientWriterInterceptorContext.throwWriterNotFoundException(ClientWriterInterceptorContext.java:40)
    at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.getWriter(AbstractWriterInterceptorContext.java:146)
    at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:121)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.writeRequestBody(ClientInvocation.java:394)
    at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.writeRequestBodyToOutputStream(ApacheHttpClient4Engine.java:666)
    at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.buildEntity(ApacheHttpClient4Engine.java:631)
    at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.loadHttpMethod(ApacheHttpClient4Engine.java:509)
    at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:310)
    ... 16 more

二、问题分析

1)从异常错误信息看调用REST服务端的代码变成了RESTEasy,而不是原正常的Jersey咯

2)在网上找问题看到一位同学讲把clientConfig参数去掉可解决问题,讲的应该有道这个clientConfig是Jersey包的代码而从异常错信息来看客户端用的是RESTEasy的代码;不过我的有这个clientConfig配置,所以我并没有去验证这位同学讲的解决方法;

3)创建代码,如下:

Client client = ClientBuilder.newBuilder(clientConfig)
      .register(feature)
      .build();

4)阅读 ClientBuilder.newBuilder() 源码发现,ClientBuilder的构建是这样子的:会加载 META-INF/services/javax.ws.rs.client.ClientBuilder 文件的的实现类并使用,如果在META-INF/services/javax.ws.rs.client.ClientBuilder 文件中未加载则默认使用 org.glassfish.jersey.client.JerseyClientBuilder 类;从这个逻辑中看出来之前rest服务正常调用是没有引入RESTEasy jar包,肯定设置使用的是Jersey,猜测RESTEasy jar包中应该有 META-INF/services/javax.ws.rs.client.ClientBuilder 文件并配置了RESTEasy的ClientBuilder的继承类才会导致JAX-RS加载到RESTEasy并使用RESTEasy作为客户端调用REST服务;

5)接着查找一下是否有RESTEasy的META-INF/services/javax.ws.rs.client.ClientBuilder,结果是有的,如下:

6)问题确认了我们就好处理咯。

三、问题解决

1)修改客户代码,指定使用Jersey,如下:

Client client = ClientBuilder.newBuilder(clientConfig)
      .register(feature)
      .build();

改为(直接指定使用JerseyClientBuilder.createClient(...)创建,这样、JAX-RX就不会根据 META-INF/services/javax.ws.rs.client.ClientBuilder 文件中去加载了)

Client client = JerseyClientBuilder.createClient(clientConfig)
   .register(feature);

 2)修改后,再次调用测试,能正常调用并响应正常。

 

 

 

end.

posted on 2018-10-13 15:18  流易  阅读(1158)  评论(0编辑  收藏  举报