我正在尝试使用 RestTemplate 实现对身份验证服务器的 Rest 调用,并在服务器返回异常的情况下记录响应。为了做到这一点,我使用了 ResponseEntityExceptionHandler 来处理 HttpClientErrorException 和 HttpServerErrorException。
public Response sendRequest(Request requestBody, String url, HttpMethod httpMethod, Response responseBody) {
RestTemplate restTemplate = new RestTemplate();
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(requestBody.getBody(),
this.securityHeaders);
ResponseEntity<? extends Response> response = restTemplate.exchange(url, httpMethod, request,
responseBody.getClass());
return response.getBody();
}
Run Code Online (Sandbox Code Playgroud)
目前我的 ResponseEntityExceptionHandler 如下
@ControllerAdvice
public class RestResponseExceptionHandler extends ResponseEntityExceptionHandler {
private static Logger log = LoggerFactory.getLogger(RestResponseExceptionHandler.class);
private HttpHeaders headers = new HttpHeaders();
@ExceptionHandler(value = { HttpClientErrorException.class, HttpServerErrorException.class })
protected ResponseEntity<Object> handleConflict(HttpStatusCodeException ex, WebRequest request) { …Run Code Online (Sandbox Code Playgroud)