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


相关文章
|
6月前
|
人工智能 运维 安全
Skill即服务:用Agent安全玩转云上Flink
Flink Skill是阿里云为AI Agent时代打造的安全运维能力,通过Confirm门控、目标锁定、Read-back验证三层防护,实现自然语言驱动的Flink全生命周期管理。实测可将作业反压从99%修复至0%,全域巡检缩至30秒,并支持多Skill协同搭建实时数仓等复杂场景。
1017 2
|
9月前
|
人工智能 算法 测试技术
从工具到搭档:深度拆解 Claude Code 的五大核心机制与实战心法
用了一段时间 Claude Code 之后,我越来越觉得它和传统的 AI 编程助手不是一个物种。大多数 AI 编程工具本质上是"补全器"——你写半行代码,它帮你续写后半行。而 Claude Code 更像是一个能理解整个项目的"系统级协作者",它拥有超过 200K token 的上下文窗口,意味着它可以一次性"阅读"你项目中成百上千个文件,真正理解代码之间的依赖关系。 这篇文章不是官方文档的翻译,而是我在实际使用过程中对其核心架构和最佳实践的理解与总结。
5322 1
|
11月前
|
存储 人工智能 数据库
到底什么是AI Agent?
Agent是具备感知、决策与行动能力的智能体,通过大模型(LLM)结合记忆(Memory)和工具(Tools)调用,实现自主规划与执行任务,如小爱同学自动点餐。其核心为:LLM + Memory + Tools + Planning。
3967 7
|
JSON 数据格式
FeignClient【问题】Method threw ‘feign.codec.DecodeException‘ exception.也许是最简单的解决方法
FeignClient【问题】Method threw ‘feign.codec.DecodeException‘ exception.也许是最简单的解决方法
1852 0
|
XML 前端开发 Java
Spring MVC 父子容器是什么?这篇文章讲清楚了
Spring MVC 父子容器是初学 Spring MVC 时最先接触到 Spring 知识点之一,还记得我刚工作那会,项目基础架构是其他同事搭建的,其中就用到了 Spring MVC 中的父子容器,还把 Spring MVC 中的不同层拆成了不同的 maven 模块。这里暂不讨论这种模块拆分方式的优劣,Spring 为什么设计出具有层次结构的容器呢?Web 环境中什么场景会用到这种具有层次结构的容器?
1632 0
Spring MVC 父子容器是什么?这篇文章讲清楚了
|
Linux 数据库
在Linux中,什么是冷备份和热备份?
在Linux中,什么是冷备份和热备份?
|
JSON Java 数据格式
Could not extract response: no suitable HttpMessageConverter found for ..content type [text/html...]
Could not extract response: no suitable HttpMessageConverter found for ..content type [text/html...]
1907 0

热门文章

最新文章