RestTemplate请求UnknownContentTypeException:no suitable HttpMessageConverter异常

简介: RestTemplate请求UnknownContentTypeException:no suitable HttpMessageConverter异常

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;
    }


相关文章
Kam
|
Java Maven
Feign调用把GET请求自动转成POST请求解决:Request method 'POST' not supported
Feign调用把GET请求自动转成POST请求解决:Request method 'POST' not supported
Kam
2199 0
|
8月前
|
JSON 数据格式
FeignClient【问题】Method threw ‘feign.codec.DecodeException‘ exception.也许是最简单的解决方法
FeignClient【问题】Method threw ‘feign.codec.DecodeException‘ exception.也许是最简单的解决方法
413 0
Bad Request, Resolved [org.springframework.http.converter.HttpMessageNotReadableException,跟着视频仔细比对
Bad Request, Resolved [org.springframework.http.converter.HttpMessageNotReadableException,跟着视频仔细比对
|
8月前
|
前端开发 Java Spring
Spring MVC 出现Request method ‘GET‘ not supported解决办法
Spring MVC 出现Request method ‘GET‘ not supported解决办法
|
JSON 前端开发 Java
SpringMVC中@RequestBody和@ResponseBody两个注解的区别
📒 程序员小王的博客:程序员小王的博客 🎉 欢迎点赞 👍 收藏 ⭐留言 📝 😊 如有编辑错误联系作者,如果有比较好的文章欢迎分享给我,我会取其精华去其糟粕 🍅java自学的学习路线:java自学的学习路线 今天我想详细的了解一下Spring MVC中@RequestBody和@ResponseBody的区别,让我自己在开发中更好的使用这两个注解
233 0
SpringMVC中@RequestBody和@ResponseBody两个注解的区别
|
Java
【Java异常】Feign常见的坑总结之一:Method Not Allowed“,“message“:“Request method ‘POST‘ not supported“,“path“:“/*
【Java异常】Feign常见的坑总结之一:Method Not Allowed“,“message“:“Request method ‘POST‘ not supported“,“path“:“/*
307 0
|
Java Spring
使用feign调用的时候出现Could not autowire. No beans of ‘Api2UserFegin‘ type found报错
使用feign调用的时候出现Could not autowire. No beans of ‘Api2UserFegin‘ type found报错
351 0
|
JSON 前端开发 Java
http请求注解之@RestController
http请求注解之@RestController
|
Java Spring
【feign】Could not write request: no suitable HttpMessageConverter found for request type
【feign】Could not write request: no suitable HttpMessageConverter found for request type
648 0
|
Java Spring
Java中restTemplate携带Header请求
Java中restTemplate携带Header请求
214 2