【发布时间】:2016-05-02 14:08:14
【问题描述】:
我正在使用 Spring REST 来处理我的请求。 这是我的代码示例:
RestTemplate restTemplate = new RestTemplate();
HttpEntity entity = new HttpEntity(headers);
ResponseEntity<String> responseEntity = null;
try {
responseEntity = restTemplate.exchange(apiAddress + "action?uniqueId=" + id, HttpMethod.GET, entity, String.class);
}catch (RestClientException ex) {
System.out.println(responseEntity.toString());
String errorMessage = ex.getMessage();
}
当我获得 200 个状态和带有返回值的 JSON 时,一切正常。
例如,问题在于 404 JSON。
在调试过程中,我发现当 404 发生时,我的 responseEntity 仍然为空,因此我无法获取错误代码。此外,我无法从我知道它正在发送的服务器获得 JSON 回复。
我尝试了 HTTP 请求器,它可以很好地处理 200 个响应 - 给我请求的数据和 404 个响应 - 给我一个带有错误描述的 JSON。
最好的问候, 卡罗尔
【问题讨论】: