无法提取响应:找不到适合于响应类型类和内容类型[text / html]的HttpMessageConverter

时间:2019-10-15 10:44:03

标签: java spring-boot resttemplate

我在通过Rest Api发布请求时抛出,结果是

  

Content-Type:应用程序/ json

{
"error": "file content is not in the appropriate format",
"file_name": "15-10-2019-11-19-32_KKK.csv"
}

当我尝试使用我的代码时,我使用下面的代码

public RestTemplate restTemplateBuilder() {
    RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
    converter.setObjectMapper(mapper);
    restTemplate.getMessageConverters().add(0, converter);
    restTemplate.setErrorHandler(new RestTemplateHandler());
    return restTemplate;
  }
RestTemplate restTemplate = restTemplateBuilder();

ResponseEntity<FileUploadResponseDTO> serviceResponse = restTemplate.exchange(url, HttpMethod.POST, requestEntity, FileUploadResponseDTO.class);

org.springframework.web.client.RestClientException:无法提取响应:找不到适用于响应类型[.... FileUploadResponseDTO]和内容类型[text / html]的HttpMessageConverter 造成故障。如何转换?

1 个答案:

答案 0 :(得分:0)

您可以将内容类型标题添加到RestTemplate:

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> requestEntity = new HttpEntity<String>(headers);