RestTemplate HttpMessageConverter報錯的解決方案no suitable HttpMessageConverter


錯誤

no suitable HttpMessageConverter found for response type and content type [text/html;charset=UTF-8]

這邊調用的時候使用了RestTemplate

使用過程

RestTemplate restTemplate = new RestTemplate();
String payUrlFinal = "http://127.0.0.1/pay?orderId=1";
PayResponse payResponse = restTemplate.getForObject(payUrlFinal, PayResponse.class);

下面是我請求的路徑

 @GetMapping("/pay")
    @ResponseBody
    public PayResponse pay(@RequestParam("orderId") String orderId){
        //1.查詢訂單
        OrderDTO orderDTO = orderService.findOne(orderId);
        if(Objects.isNull(orderDTO)){
            //訂單不存在
            throw new SellException(ResultEnum.ORDER_NOT_EXIST);
        }
 
        //發起支付
        PayResponse payResponse = payService.create(orderDTO);
        return payResponse;
    }

調用之后程序報以下錯誤

 Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.lly835.bestpay.model.PayResponse] and content type [text/html;charset=UTF-8]] with root cause
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.lly835.bestpay.model.PayResponse] and content type [text/html;charset=UTF-8]
	at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109)
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655)
	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
	at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287)
	at com.imooc.controller.PayController.create(PayController.java:107)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)

解決方案:

我們繼承 MappingJackson2HttpMessageConverter 並在構造過程中設置其支持的 MediaType 類型即可:

public class WxMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
    public WxMappingJackson2HttpMessageConverter(){
        List<MediaType> mediaTypes = new ArrayList<>();
        mediaTypes.add(MediaType.TEXT_PLAIN);
        mediaTypes.add(MediaType.TEXT_HTML);  //加入text/html類型的支持
        setSupportedMediaTypes(mediaTypes);// tag6
    }
}

然后把這個 WxMappingJackson2HttpMessageConverter 追加到 RestTemplate 的 messageConverters 消息轉換鏈中去:

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new WxMappingJackson2HttpMessageConverter());
String payUrlFinal = "http://127.0.0.1/pay?orderId=1";
PayResponse payResponse = restTemplate.getForObject(payUrlFinal, PayResponse.class);

具體原因分析可參照下面的地址:

http://blog.csdn.net/kinginblue/article/details/52706155

參考

原文:https://blog.csdn.net/u011768325/article/details/77097655


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM