我正在调用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中提取的响应主体:
修改:当我调用一个获得400错误的方法时,正文会正确填充,所以这似乎仅限于某些状态?