如何在401错误后抛出RestClientException后获取响应体

时间:2016-07-29 15:20:34

标签: java spring rest

我正在调用Magento REST服务,如果出现错误,该服务会返回一条消息。对于401错误,响应正文应为{"message":"You did not sign in correctly or your account is temporarily disabled."})。使用RestTemplate如何进入身体?

public GetTokenResponse call(String url, GetTokenRequest request)
{
    RestTemplate restTemplate = new RestTemplate();
    try {
        ResponseEntity<GetTokenResponse> result = restTemplate.postForEntity(url, request, GetTokenResponse.class);
        // throws RestClientException on 401 error
        return result.getBody();
    } catch (HttpClientErrorException e) {
        // responseBody shows empty
        throw new UnrecoverableException(e);
    }
}

我知道从服务器返回的有效负载中存在响应主体。这是我从WireShark中提取的响应主体: enter image description here

修改:当我调用一个获得400错误的方法时,正文会正确填充,所以这似乎仅限于某些状态?

0 个答案:

没有答案