当前位置: 代码迷 >> 综合 >> no suitable HttpMessageConverter found for response type 异常
  详细解决方案

no suitable HttpMessageConverter found for response type 异常

热度:43   发布时间:2023-11-18 08:25:41.0

报错:

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

解决办法:

        HttpHeaders headers = new HttpHeaders();headers.add("Content-Type",  "text/html;charset=utf-8");HttpEntity requestEntity=new HttpEntity<>(headers);RestTemplate restTemplate = getRestTemplate(timeOut);MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();converter.setSupportedMediaTypes(Arrays.asList(MediaType.ALL));restTemplate.getMessageConverters().add(0, converter);ResponseEntity baseResultVOResponseEntity = restTemplate.exchange(url,HttpMethod.POST,requestEntity,//返回值类型new ParameterizedTypeReference<ResponseDTO<SomeResponseDTO>>() {
    });}private static RestTemplate getRestTemplate(Integer timeout) {
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();requestFactory.setConnectTimeout(timeout);return new RestTemplate(requestFactory);}

在这里插入图片描述

  相关解决方案