[英]Could not extract response: no suitable HttpMessageConverter found for response type class and content type [text/html]
I throw when post request via Rest Api, result is我通过 Rest Api 发布请求时抛出,结果是
Content-Type: application/json
内容类型:应用程序/json
{
"error": "file content is not in the appropriate format",
"file_name": "15-10-2019-11-19-32_KKK.csv"
}
When I try code on my side, I use the below code当我在我这边尝试代码时,我使用下面的代码
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: Could not extract response: no suitable HttpMessageConverter found for response type [....FileUploadResponseDTO] and content type [text/html] Gives a fault. org.springframework.web.client.RestClientException:无法提取响应:没有找到适合响应类型 [....FileUploadResponseDTO] 和内容类型 [text/html] 的 HttpMessageConverter 给出错误。 how do I convert?
我该如何转换?
You can add content-type header to RestTemplate:您可以将内容类型 header 添加到 RestTemplate:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> requestEntity = new HttpEntity<String>(headers);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.