Http响应Content-Type为json。错误的“无效的MIME类型”

时间:2018-10-25 11:14:28

标签: java spring http spring-boot resttemplate

出于某种目的,我正在使用第三方API。

此API使用URL进行响应,该URL基本上是一个Amazon AWS URL,它实际上具有我感兴趣的数据。

我能够通过浏览器和Postman调用AWS URL,后者会响应数据。

但是通过代码,我收到以下错误:

InvalidMimeTypeException: Invalid mime type "json": does not contain '/'

我正在使用Spring Boot并进行http calsing,我正在使用RestTemplate

响应头:

accept-ranges →bytes
content-length →1428
content-type →json
date →Thu, 25 Oct 2018 10:05:57 GMT
last-modified →Wed, 24 Oct 2018 18:44:44 GMT
server →AmazonS3

Content-Type为“ json”

我使用的代码:

protected HttpHeaders httpHeader(String encodedApiKey) {
    HttpHeaders headers = new HttpHeaders();
    headers.set("Content-Type", " */*");
    headers.add(HttpHeaders.ACCEPT, " */*");
    headers.add(HttpHeaders.ACCEPT_CHARSET, StandardCharsets.UTF_8.name());

    if (encodedApiKey != null && !encodedApiKey.isEmpty()) {
      headers.add("Authorization", "Basic " + encodedApiKey);
    }
    return headers;
  }

  private HttpEntity<Tickets> getHttpEntity(String apiKey) {
    if (apiKey== null || apiKey.isEmpty()) {
      return new HttpEntity<>(httpHeader(null));
    }
    return new HttpEntity<>(httpHeader(encodeBase64(apiKey)));
  }

 ResponseEntity<Tickets> responseEntity = restTemplate.exchange(awsUri,
 HttpMethod.GET,
 httpEntity, new ParameterizedTypeReference<Tickets>() {});
}

这是一个例外:

org.springframework.http.InvalidMediaTypeException: Invalid mime type "json": does not contain '/'
    at org.springframework.http.MediaType.parseMediaType(MediaType.java:534)
    at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:869)
    at org.springframework.web.client.HttpMessageConverterExtractor.getContentType(HttpMessageConverterExtractor.java:124)
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:88)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:959)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:942)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:689)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:662)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:339)
  1. 我做错了什么?
  2. 我尝试使用ClientHttpRequestInterceptor,但这不会修改服务器的响应标头(或将 Content-Type更改为 application / json

有人有什么主意吗?

2 个答案:

答案 0 :(得分:1)

我没有亲自检查此案,但与您的案子极端相似。因为在错误堆栈HttpMessageConverterExtractor中存在,所以我可以假设Spring不知道如何转换响应。因此,您可以自己添加属性MessageConverter

protected HttpHeaders httpHeader(String encodedApiKey) {
 /***/
 restTemplate.setMessageConverters(getMessageConverters());
 ResponseEntity<Tickets> responseEntity = restTemplate.exchange(awsUri,
 HttpMethod.GET,
 httpEntity, new ParameterizedTypeReference<Tickets>() {}); 
}

private List<HttpMessageConverter<?>> getMessageConverters() {
    List<HttpMessageConverter<?>> converters = 
      new ArrayList<HttpMessageConverter<?>>();
    converters.add(new MappingJackson2HttpMessageConverter());
    return converters;
}

Here is a full explanation

UPD ResponseExctractor示例

private class TicketsResponseExtractor extends HttpMessageConverterExtractor<Tickets> {

    public TicketsResponseExtractor (Class<Tickets> responseType,
      List<HttpMessageConverter<?>> messageConverters) {
        super(responseType, messageConverters);
    }

    @Override
    public Tickets extractData(ClientHttpResponse response) throws IOException {

        if (response.getStatusCode() == HttpStatus.OK) {
            response.getHeaders().set("Content-Type", MediaType.APPLICATION_JSON_VALUE);
            return super.extractData(response);             
        } else {
            return null;
        }
    }
}

RestTemplate通话:

ResponseExtractor<Tickets> responseExtractor =
    new TicketsResponseExtractor(Tickets.class, restTemplate.getMessageConverters());
Tickets result =
    restTemplate.execute(awsUri, HttpMethod.GET, null, responseExtractor);

答案 1 :(得分:-2)