1、场景:
springboot项目, 使用 spring 自家封装的 RestTemplate 来 远程调用接口
2、报错日志:
14:59:19.720 [http-nio-8080-exec-10] ERROR c.r.f.w.e.GlobalExceptionHandler - [handleRuntimeException,69] - 请求地址'/task/jobInfo/list',发生未知异常.
org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [interface java.util.List] and content type [text/html;charset=UTF-8]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:126)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:778)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:437)
at com.hidata.devops.selfops.service.impl.HiJobOperateService.getVersionRecallRemark(HiJobOperateService.java:470)
at com.hidata.devops.selfops.service.impl.OpsHijobInfoServiceImpl.selectOpsHijobInfoList(OpsHijobInfoServiceImpl.java:119)
at com.hidata.devops.selfops.controller.OpsHijobInfoController.list(OpsHijobInfoController.java:44)
at com.hidata.devops.selfops.controller.OpsHijobInfoController$$FastClassBySpringCGLIB$$691faf83.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692)
at com.hidata.devops.selfops.controller.OpsHijobInfoController$$EnhancerBySpringCGLIB$$239b4905.list(<generated>)
at sun.reflect.GeneratedMethodAccessor932.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141)
3、分析日志,异常原因
RestTemplate请求不支持content type [text/html;charset=UTF-8]类型
4、解决办法
Springboot注入RestTemplate类,追踪RestTemplate 实例化过程发现默认的RestTemplate 只支持application/json格式,所以需要手动补充text/html格式
查看源码,如下:
@Bean("restTemplate")
@Primary
public RestTemplate restTemplate(){
RestTemplate restTemplate = new RestTemplate();
return restTemplate;
}
public RestTemplate() {
......
if (jackson2Present) {
this.messageConverters.add(new MappingJackson2HttpMessageConverter());
}
.....
}
public MappingJackson2HttpMessageConverter() {
this(Jackson2ObjectMapperBuilder.json().build());
}
public MappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
super(objectMapper, MediaType.APPLICATION_JSON, new MediaType("application", "*+json"));
}
解决方案:
增加支持的MediaType类型:支持text/plan,text/html格式
@Bean("restTemplate")
public RestTemplate restTemplate(){
RestTemplate restTemplate = new RestTemplate();
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(
MediaType.TEXT_HTML,
MediaType.TEXT_PLAIN));
restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
return restTemplate;
}