no suitable HttpMessageConverter found for response type [X] and content type [text/plain]
阿新 • • 發佈:2021-01-23
技術標籤:Javajavaspringspring boot
在後端使用springBoot提供的RestTemplate傳送請求獲取響應時報錯:org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class java.lang.Object] and content type [text/plain]
檢視報錯提示:找不到合適的HttpMessage 轉換器,在瀏覽器中,瀏覽器可以根據content-type 自動將接收到的響應資料轉成合適的資料展示,如頁面,圖片等,但是後端沒有瀏覽器的功能,只能自己對響應做轉換,將響應的資料轉換成Json格式資料或者字串等等,當然也可以自己寫個裝換器轉成自己想要的物件格式。
這裡我是使用原有的轉換器將響應資料轉換成json字串了。
RestTemplate restTemplate = new RestTemplate(); MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM)); restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter); String result = restTemplate.getForObject(url, String.class, params);
感謝:https://blog.csdn.net/sinat_34763749/article/details/82716475