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

     

  • 相关阅读:
    c#中的Unity容器
    c# Task 篇幅二
    C# Task 篇幅一
    多线程Thread,线程池ThreadPool
    第十二章:二叉查找树(1)
    第十章:基本数据结构(2)
    第十章:基本数据结构(1)
    第十章:基本数据结构(0)
    劝学
    CentOS7安装Docker CE
  • 原文地址:https://www.cnblogs.com/wobuchifanqie/p/14074827.html
Copyright © 2011-2022 走看看