RestClientException: Could not extract response: no suitable HttpMessageConverter found for response

Posted 醇氧

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RestClientException: Could not extract response: no suitable HttpMessageConverter found for response相关的知识,希望对你有一定的参考价值。

今天请求百度的鹰眼服务返回下面的错误:

 


org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.GetDistanceResultDto] and content type [text/javascript;charset=UTF-8]

	at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:121)
	at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:994)
	at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:977)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736)
	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669)
	at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:607)
	at com.hebeiwanteng.drivingserver.basic.utils.HttpRequsetUtil.exchange(HttpRequsetUtil.java:84)

 

无法提取响应:没有为响应类型[class com..getDistanceResultTo]和内容类型[text/javascript]找到合适的HttpMessageConverter;字符集=UTF-8]

这一下就知道怎么处理了  在 HttpMessageConverter  增加 MediaType 是  [text/javascript] 类型的;

代码如下:

private static final RestTemplate restTemplate = createRestTemplate();

    synchronized static RestTemplate createRestTemplate() 
        RestTemplate restTemplate = new RestTemplate();
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(
                MediaType.APPLICATION_JSON_UTF8,
                MediaType.APPLICATION_JSON,
                MediaType.TEXT_html,
                MediaType.TEXT_PLAIN));
        restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
        return restTemplate;
    

重点来了:

org.springframework.http.MediaType 中没有   [text/javascript] .

 

所以需要自己定义代码如下:

 

      MediaType textMediaType = MediaType.parseMediaType("text/javascript;charset=UTF-8");

 

把自己定义的 textMediaType  添加到上面自己定义的 MappingJackson2HttpMessageConverter 中;

 

    synchronized static RestTemplate createRestTemplate() 
        RestTemplate restTemplate = new RestTemplate();
        MediaType textMediaType = MediaType.parseMediaType("text/javascript;charset=UTF-8");
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(
                MediaType.APPLICATION_JSON_UTF8,
                MediaType.APPLICATION_JSON,
                MediaType.TEXT_HTML,
                textMediaType,
                MediaType.TEXT_PLAIN));
        restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
        return restTemplate;
    

 

 

 

 

 

 

 

 

以上是关于RestClientException: Could not extract response: no suitable HttpMessageConverter found for response的主要内容,如果未能解决你的问题,请参考以下文章

RestClientException: Could not extract response: no suitable HttpMessageConverter found for response

RestClientException: Could not extract response: no suitable HttpMessageConverter found for response

PHP Humaniser l'affichagededuréesécoulées

single-number

*Linked List Random Node

找不到持久模块