RestTemplate 使用 ResourceAccessException 获取正文

RestTemplate get body with ResourceAccessException

我正在发送 API 并接收状态代码 400 以及我需要解析的正文

使用 时,我未能解析响应:

try {
     ResponseEntity<ResponseVO> response = restTemplate.exchange(uri, HttpMethod.POST, request, ResponseVO.class);
} catch(HttpStatusCodeException e){
     // not catch
     String errorpayload = e.getResponseBodyAsString();
}  catch (RestClientException e) {  
    // catch without body
}

此外,通过添加错误处理程序(默认和特定)建议我总是得到一个 ResourceAccessException,它不会被 HttpClientErrorException 捕获并且不包含 body/headers 数据

在那种情况下我怎样才能得到 body/headers?我必须使用 RestTemplate 的替代品吗?

此外,当我在 handleError:

中捕获异常时,我如何 return 到请求的上下文
 public void handleError(ClientHttpResponse response) throws IOException {

我使用拦截器的问题已经读取了我的 body,

将 RestTemplate 更改为使用 BufferingClientHttpRequestFactory 我现在可以 re-read 我的 body

RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new HttpComponentsClientHttpRequestFactory()));

Using this wrapper allows for multiple reads of the response body.