我在使用 springs RestTemplate 从 AWS 托管 API 检索数据时遇到问题。
以下是我的代码
public List<AccountLocation> getCircuitViewRooms(String clientApiKey) {
String uri = circuitApiUrl + "school/rooms?api_key=" + clientApiKey;
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().clear();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
List<AccountLocation> locations = new ArrayList<>();
HttpEntity entity = getHeaders();
ResponseEntity<?> resp = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
/*if(resp.getStatusCode() == HttpStatus.OK) {
CircuitViewRooms[] rooms = resp.getBody();
for(CircuitViewRooms room : rooms) {
AccountLocation loc = new AccountLocation();
loc.setLocationName(room.getLaundryRooms().getLaundry_room_name());
loc.setLocationId(room.getLocation());
locations.add(loc);
}
}*/
return locations;
}
private HttpEntity<?> getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
headers.add("x-api-key", xApiKey);
return new HttpEntity<>(null, headers);
}
我收到 200 OK 响应,但当它尝试使用 httpMessageConverterExtractor 时,它会抛出 org.springframework.http.InvalidMediaTypeException: Invalid mime type "method.request.header.Content-Type": does not contains '/'
堆栈跟踪
at org.springframework.http.MediaType.parseMediaType(MediaType.java:534) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:924) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.client.HttpMessageConverterExtractor.getContentType(HttpMessageConverterExtractor.java:133) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:90) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:994) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:977) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:578) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at com.upp.uppstudentapp.services.laundry.account_services.CircuitViewService.getCircuitViewRooms(CircuitViewService.java:38) ~[main/:na]
Postman 返回,但我还看到响应 header 中的内容类型是“method.request.header.Content-Type”
我也尝试过使用object.class
有谁知道解决这个问题的方法或解决方案吗?
这是 api 的文档片段
One notable difference is this implementation has the ability to produce output in either XML or JSON formats whereas the previous API produced only XML. Additionally, method name parameters are not used in this implementation - the full API path determines the applicable method.
编辑 1:添加的内容类型尝试相同的异常
private HttpEntity<?> getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
headers.add("Content-Type", "application/json");
//headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("x-api-key", xApiKey);
return new HttpEntity<>(null, headers);
}
还尝试过 setContent。
最佳答案
setContentType
期望 MediaType
,对于 JSON 添加:
headers.setContentType(MediaType.APPLICATION_JSON);
关于java - 由于响应内容类型无效,其余模板交换失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59665172/