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


相关文章
|
4月前
|
Java Spring
spring restTemplate 进行http请求的工具类封装
spring restTemplate 进行http请求的工具类封装
202 3
Kam
|
Java Maven
Feign调用把GET请求自动转成POST请求解决:Request method 'POST' not supported
Feign调用把GET请求自动转成POST请求解决:Request method 'POST' not supported
Kam
2099 0
Bad Request, Resolved [org.springframework.http.converter.HttpMessageNotReadableException,跟着视频仔细比对
Bad Request, Resolved [org.springframework.http.converter.HttpMessageNotReadableException,跟着视频仔细比对
|
6月前
|
JSON 数据格式
FeignClient【问题】Method threw ‘feign.codec.DecodeException‘ exception.也许是最简单的解决方法
FeignClient【问题】Method threw ‘feign.codec.DecodeException‘ exception.也许是最简单的解决方法
314 0
|
中间件 PHP
POST 419 ; message: “CSRF token mismatch.“, exception: “Symfony\Component\HttpKernel\Exception\HttpE
POST 419 ; message: “CSRF token mismatch.“, exception: “Symfony\Component\HttpKernel\Exception\HttpE
129 0
|
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“:“/*
252 0
|
Java Spring
required a bean of type ‘org.springframework.web.client.RestTemplate‘ that could not be found.
required a bean of type ‘org.springframework.web.client.RestTemplate‘ that could not be found.
required a bean of type ‘org.springframework.web.client.RestTemplate‘ that could not be found.
|
Java Spring
使用feign调用的时候出现Could not autowire. No beans of ‘Api2UserFegin‘ type found报错
使用feign调用的时候出现Could not autowire. No beans of ‘Api2UserFegin‘ type found报错
317 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
567 0