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

    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

     

  • 相关阅读:
    BZOJ 3236 AHOI 2013 作业 莫队算法
    使用再哈希算法查找元素
    冰雪奇缘--用爱酿就一部经典
    Scrapy系列教程(2)------Item(结构化数据存储结构)
    html学习笔记二
    知方可补不足~sqlserver中触发器的使用
    我心中的核心组件(可插拔的AOP)~调度组件quartz.net
    EF架构~为BulkInsert引入SET IDENTITY_INSERT ON功能
    知方可补不足~sqlserver中使用sp_who查看sql的进程
    知方可补不足~为千万级数据表加索引
  • 原文地址:https://www.cnblogs.com/wobuchifanqie/p/14074827.html
Copyright © 2011-2022 走看看