欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

no suitable HttpMessageConverter found for response type 异常处理

程序员文章站 2022-07-10 17:30:35
报错: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();...

报错:

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);
    }

本文地址:https://blog.csdn.net/qq_37539693/article/details/110222782

相关标签: java post restful