Java異常處理008:RestTemplate請求Could not extract response: no suitable HttpMessageConverter found for response type.... content type [text/html;charset=UTF-8]異常
阿新 • • 發佈:2020-12-02
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 forresponse 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