对于一个看似超级直截了当的问题,已经困扰了几天:
我在application/json
中使用RestTemplate进行简单的GET请求,但我一直在
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:636)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:592)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:552)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:470)
我做了研究并遵循了这个tutorial,我也查看了这个POST request via RestTemplate in JSON的解决方案。但是他们都没有帮助,这是我的代码:
RestTemplate restTemplate = new RestTemplate();
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
restTemplate.exchange(endpoint, HttpMethod.GET, requestEntity, String.class);
endpoint
是http://localhost:8080/api/v1/items?itemIds=" + URLEncoder.encode(itemIds, "UTF-8")
,在邮差中效果很好。
itemIds
是逗号分隔的列表,如下所示:
5400028914,5400029138,5400029138,5400029138,5400029138,5400028401,5400028918,5400028076,5400028726
我也尝试使用getForObject
,如下所示:
String result = restTemplate.getForObject(endpoint, String.class);
这给了我这个错误:
org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type
我不确定我错过了什么或做错了什么,但是相同的端点在Postman上完美运行,但唯一的区别是我在Postman应用程序中添加了Content-Type标头。
这是我的邮递员要求:
GET /api/v1/items?itemIds=abc%2cdef%2cghi HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Postman-Token: 84790e06-86aa-fa8a-1047-238d6c931a68
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4
那么如果上面的代码错误,如何使用RestTemplate正确设置内容类型?
另一次深度探索,我已经启动Wireshark来捕获两个HTTP请求,以下是截图:
我仍然不明白为什么我的Java程序中的那个抛出400而Postman的那个工作正常。
非常感谢。
答案 0 :(得分:1)
好吧,最终,我的一位同事帮我弄明白了为什么,不管你信不信,这很简单:
endpoint
是这样的:
"http:localhost:8080/api/v1/items?itemIds=" + URLEncoder.encode(itemIds, "UTF-8");
但是,它应该是"http:localhost:8080/api/v1/items?itemIds=" + itemIds;
itemIds
只是一个以逗号分隔的列表。
URLEncoder编码经过&#34; UTF-8&#34;架构,这个以逗号分隔的列表变为itemIds=5400028914%2C5400029138%2C5400029138%2C5400029138%2C5400029138%2C5400028401%2C5400028918%2C5400028076
来自
itemIds=5400028914,5400029138,5400029138,5400029138,5400029138,5400028401,5400028918,5400028076,5400028726
使用RestTemplate时,我们不需要使用URLEncoder来编码URL,有人可以帮我加深理解吗?
谢谢!
答案 1 :(得分:0)
您不需要为GET方法设置“requestEntity”,请尝试使用以下代码:
public ItemInfo getItemInfo() throws Exception {
String url =
"http://localhost:8080/api/v1/items?itemIds=abc";
ObjectMapper objectMapper = new ObjectMapper();
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
String responseBody = response.getBody();
handlerError(response, url);
try {
return objectMapper.readValue(responseBody, ItemInfo.class);
} catch (IOException exception) {
LOGGER.error("failed to send REST request", exception);
throw new Exception(ErrorCode.NOT_AVAILABLE, url);
}
}
private void handlerError(final ResponseEntity<String> response, final String url) throws Exception {
String responseBody = response.getBody();
try {
if (RestUtil.isError(response.getStatusCode())) {
ObjectMapper objectMapper = new ObjectMapper();
MyInfoError myInfoError = objectMapper.readValue(responseBody, MyInfoError.class);
throw new Exception(infoErreur, ErrorCode.UNKNOWN_CODE_ERROR);
} else if (RestUtil.isNotFound(response.getStatusCode())) {
throw new Exception(ErrorCode.NOT_AVAILABLE, "MyService");
}
} catch (IOException exception) {
LOGGER.error("failed to send REST request", exception);
throw new Exception(ErrorCode.NOT_AVAILABLE, url);
}
}
我把它设为NULL因为GET方法没有发送任何JSON请求正文/标题:
restTemplate.exchange(url, HttpMethod.GET, null, String.class);
或者:将您的标题放在GET方法中,如下所示:
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<?> requestEntity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
对于POST的示例,您需要设置requestEntity,如下所示:
ItemFormRequest request =
new ItemFormRequest(1,"item no", "item name");
HttpEntity<ItemFormRequest> requestEntity = new HttpEntity<>(request);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
String responseBody = response.getBody();
希望这有帮助:)