【问题标题】:Could not extract response: no suitable HttpMessageConverter found for response type class and content type [text/html]无法提取响应:没有找到适合响应类型类和内容类型 [text/html] 的 HttpMessageConverter
【发布时间】:2020-02-12 00:35:29
【问题描述】:

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

内容类型:application/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 给出一个错误。如何转换?

【问题讨论】:

  • public class FileUploadResponseDTO { public String error; public String file_name; public List&lt;Long&gt; data; public String ok; -- getter setter 它工作时我将其更改为公共,并在 url 的末尾使用“/”更多。已删除

标签: java spring-boot resttemplate


【解决方案1】:

您可以在 RestTemplate 中添加 content-type 标头:

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

【讨论】:

    猜你喜欢
    • 2021-11-18
    • 2017-10-31
    • 2019-09-09
    • 2017-01-22
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    相关资源
    最近更新 更多