gpt4 book ai didi

java - Http 响应内容类型为 json。 "Invalid mime type"错误

转载 作者:行者123 更新时间:2023-12-01 06:02:57 25 4
gpt4 key购买 nike

我出于某种目的使用第 3 方 API。

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

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

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

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

我正在使用 Spring Boot 并进行 http 调用,我正在使用 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

内容类型是“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>() {});
}

这是一个异常(exception):

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,但这不会修改服务器的响应 header (或Content-Type 为application/json)

有人知道吗?

最佳答案

我没有亲自检查此案例,但它与您的案例极其相似。由于错误堆栈中存在 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 ResponseExtractor 示例

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

关于java - Http 响应内容类型为 json。 "Invalid mime type"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52987942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com