相關(guān)環(huán)境:
Spring Boot:2.0.5.RELEASE
Spring Cloud :Finchley.RELEASE
FastJson:1.2.54
配置代碼:
/**
* 單獨(dú)為feign配置fastjson
*/
@SpringBootConfiguration
public class FeignConfig {
@Bean
public EncoderfeignEncoder() {
return new SpringEncoder(feignHttpMessageConverter());
}
@Bean
public DecoderfeignDecoder() {
return new ResponseEntityDecoder(new SpringDecoder(feignHttpMessageConverter()));
}
private ObjectFactoryfeignHttpMessageConverter() {
final HttpMessageConverters httpMessageConverters =
new HttpMessageConverters(getFastJsonConverter());
return () ->httpMessageConverters;
}
private FastJsonHttpMessageConvertergetFastJsonConverter() {
FastJsonHttpMessageConverter converter =
new FastJsonHttpMessageConverter();
List supportedMediaTypes =new ArrayList<>();
// MediaType mediaTypeJson =
// MediaType.valueOf(MediaType.APPLICATION_JSON_UTF8_VALUE);
// supportedMediaTypes.add(mediaTypeJson);
MediaType mediaTypeJson =
MediaType.valueOf(MediaType.TEXT_PLAIN_VALUE +";charset=UTF-8");
supportedMediaTypes.add(mediaTypeJson);
converter.setSupportedMediaTypes(supportedMediaTypes);
FastJsonConfig config =new FastJsonConfig();
config.getSerializeConfig()
.put(JSON.class, new SwaggerJsonSerializer());
config.setSerializerFeatures(
SerializerFeature.DisableCircularReferenceDetect);
converter.setFastJsonConfig(config);
return converter;
}
}
原因:
我想大家對fastJsonHttpMessageConverters的配置應(yīng)該很熟悉了,網(wǎng)上的文章也是一大片,而我一開始也是如此只進(jìn)行了全局配置呛伴。然后在使用Feign調(diào)用服務(wù)測試時畦粮,報錯了:
feign.codec.DecodeException: Could not extract response: no suitable HttpMessageConverter found for response type [class java.lang.Object] and content type [text/plain;charset=UTF-8]
at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:169)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:133)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:76)
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)
at com.sun.proxy.$Proxy92.hello(Unknown Source)
at com.example.test.TestDB.test(TestDB.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class java.lang.Object] and content type [text/plain;charset=UTF-8]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:119)
at org.springframework.cloud.openfeign.support.SpringDecoder.decode(SpringDecoder.java:60)
at org.springframework.cloud.openfeign.support.ResponseEntityDecoder.decode(ResponseEntityDecoder.java:45)
at feign.optionals.OptionalDecoder.decode(OptionalDecoder.java:23)
at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:165)
... 35 more
后來百度發(fā)現(xiàn)是Feign不和SpringMvc共用一個HttpMessageConverter绞吁,所以需要單獨(dú)配置哗脖。因為SpringMvc默認(rèn)使用的Jackson糊肠,所以我便看到了:
@SpringBootConfiguration
public class FeignByJacksonConfig {
@Bean
public DecoderfeignDecoder() {
return new ResponseEntityDecoder(new SpringDecoder(feignHttpMessageConverter()));
}
public ObjectFactoryfeignHttpMessageConverter() {
final HttpMessageConverters httpMessageConverters =new HttpMessageConverters(new PhpMappingJackson2HttpMessageConverter());
return new ObjectFactory() {
@Override
public HttpMessageConvertersgetObject()throws BeansException {
return httpMessageConverters;
}
};
}
public class PhpMappingJackson2HttpMessageConverterextends MappingJackson2HttpMessageConverter {
PhpMappingJackson2HttpMessageConverter(){
List mediaTypes =new ArrayList<>();
mediaTypes.add(MediaType.valueOf(MediaType.TEXT_PLAIN_VALUE +";charset=UTF-8")); //關(guān)鍵
setSupportedMediaTypes(mediaTypes);
}
}
}
他們通過繼承MappingJackson2HttpMessageConverter去改變MediaType闸盔,而結(jié)果也是成功的悯辙。但是為了和全局統(tǒng)一配置(也為了我的強(qiáng)迫癥),我便想著如何給Feign配置fastjson迎吵,后來找到了一篇文章(會在文末給出鏈接)躲撰,但使用后發(fā)現(xiàn)依然錯誤。百思不得其解之時击费,發(fā)現(xiàn)了SpringDecoder和SpringEncoder的不同:
FeignClientsConfiguration初始化配置中:
默認(rèn)Encoder類型為SpringEncoder拢蛋,功能為調(diào)用Spring的HttpMessageConverters處理參數(shù)。
默認(rèn)Decoder類型為ResponseEntityDecoder蔫巩,它包裝了SpringDecoder瓤狐,做的事情也就是調(diào)用Spring的HttpMessageConverters處理接口返回值。
@Bean
public EncoderfeignEncoder() {
return new SpringEncoder(feignHttpMessageConverter());
}
原來一開始配置的SpringEncoder只處理了請求時的參數(shù)批幌,對于返回值仍未作更改础锐,所以后來我加上了SpringDecoder后就成功了:
@Bean
public DecoderfeignDecoder() {
return new ResponseEntityDecoder(new SpringDecoder(feignHttpMessageConverter()));
}
參考文章鏈接:
https://blog.csdn.net/synsdeng/article/details/78380552