Java异常处理008:RestTemplate请求Could not extract response: no suitable HttpMessageConverter found for response type.... content type [text/html;charset=UTF-8]异常

摘要:
Java异常处理008:RestTemplate请求无法提取响应:nosuitableHttpMessageConverterfoundforresponsetype…contenttype[text/html;charset=UTF-8]异常开始1-异常日志:2020-12-0216:42:39.386ERROR6180---[nio-8080-

Java异常处理008:RestTemplate请求Could not extract response: no suitable HttpMessageConverter found for response type....  content type [text/html;charset=UTF-8]异常

start

  1-异常日志:

2020-12-02 16:42:39.386 ERROR 6180 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.tyj.study.rest_template.ResponseB] and content type [text/html;charset=UTF-8]] with root cause

org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.tyj.study.rest_template.ResponseB] and content type [text/html;charset=UTF-8]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:126) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:998) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:981) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:741) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:342) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
......

  2-异常原因:即RestTemplate请求不支持content type [text/html;charset=UTF-8]类型


  3-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"));
    }
 4-解决方案
  支持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;
    }
 

end

 

免责声明:文章转载自《Java异常处理008:RestTemplate请求Could not extract response: no suitable HttpMessageConverter found for response type.... content type [text/html;charset=UTF-8]异常》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Cocos Creator 配合Tiled地图的使用AES五种加密模式(CBC、ECB、CTR、OCF、CFB)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

java异常处理中的return和throw

如果把return和throw放在一起,直接会提示错误。”Unreachable statement”(无法被执行). 然而finally却可以成功骗过编译器让两者并存(是不是可以算是编译器的一个小bug呢),结果是后执行的会覆盖前者。 finally如果有return会覆盖catch里的throw,同样如果finally里有throw会覆盖catch...

CSE(Corrupted State Exceptions) 严重异常处理办法

原因分析 出现这个问题说明.NET版本至少是4.0,因为微软在.NET 4.0版本中更改了异常处理机制。微软认为catch(Exception)这种写法是不负责任的,程序员应该按照异常严重类别决定程序是否继续执行。然而事实是catch(Exception)遍地开花,程序出现异常后继续顽强地执行,然后内存报错,系统报错,蓝屏,用户来一句"破系统"。现在微软不...

PL/SQL轻量版(三)——游标与异常处理

一、游标   1.概念     游标是一个 指向上下文的句柄( handle) 或指针。通过游标,PL/SQL 可以控制上下文区和处理语句时上下文区会发生些什么事情。   2.游标处理     处理显式游标     主要包含以下四个步骤:       1.定义游标 CURSOR cursor_name[(parameter[, parameter]…)]...

MSIL实用指南-生成异常处理

本篇讲解怎么生成异常。C# 异常处理时建立在四个关键词之上的:try、catch、finally 和 throw。 一、异常的抛出抛出异常在C#语言中要使用throw关键字,使用方法是throw <一个异常对象>;和throw; 1、抛出异常抛出异常的指令是Throw,它对应的C#使用语句是throw new <异常类型>; 它的使...

SpringBoot远程接口调用-RestTemplate使用

在web服务中,调度远程url是常见的使用场景,最初多采用原生的HttpClient,现采用Spring整合的RestTemplate工具类进行.实操如下:   1. 配置 主要用以配置远程链接的相关参数. @Configuration public classRestTemplateConfig { @Bean publicRest...

Remoting异常处理:在客户端获取服务器内部错误

Remoting异常处理:在客户端获取服务器内部错误 (转载于http://blog.csdn.net/oneiter/article/details/1616369) 用Remoting技术进行开发有些日子了,其中有个问题一直困扰着我,那就是有关在客户端获取服务器内部错误的问题。当服务器和客户端部署在同一台机器上时,服务器端的错误能够在客户端成功地捕获...