简体   繁体   English

ResponseEntityExceptionHandler 为 401 异常返回空响应正文

[英]ResponseEntityExceptionHandler returns empty response body for 401 exceptions

I'm trying to implement a Rest call to an authentication server using RestTemplate and log the response in case the server returns an exception.我正在尝试使用 RestTemplate 实现对身份验证服务器的 Rest 调用,并在服务器返回异常的情况下记录响应。 In order to do this, I used a ResponseEntityExceptionHandler to handle HttpClientErrorException's and HttpServerErrorException's.为了做到这一点,我使用了 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();
}

Currently my ResponseEntityExceptionHandler is the following目前我的 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) {
    ErrorMessage responseBody = new ErrorMessage();
    try {
        JSONObject bodyOfException = new JSONObject(ex.getResponseBodyAsString());
        log.warn("{}| {}", ex.getStatusCode(), bodyOfException);
        responseBody.setErrorAndDescription(bodyOfException.optString("error"),
                bodyOfException.optString("error_description"));
    } catch (JSONException e) {
        log.error("{}| Bad authentication response body | Response Body | {}", ex.getStatusCode(),
                ex.getResponseBodyAsString());
    }

    return handleExceptionInternal(ex, responseBody, headers, ex.getStatusCode(), request);
}

} }

However, only for HttpStatus 401 exceptions, I get an empty Response Body.但是,仅对于 HttpStatus 401 异常,我得到一个空的响应正文。

Example of logs:日志示例:

400| {"error_description":"Bad client credentials","error":"invalid_client"}

401| Bad authentication response body | Response Body | 

Does anyone know how I can obtain the response body for 401 exceptions?有谁知道我如何获得 401 异常的响应正文?

Edit: When using a Postman to call the Authentication Server I get non null 401 responseBody:编辑:当使用邮递员调用身份验证服务器时,我得到非空 401 responseBody:

{
 "error": "invalid_token",
 "error_description": "Token was not recognised"
}

I was finally able to log the response body for 401 Exceptions. 我终于能够记录响应正文的“ 401异常”。 I added the dependency 我添加了依赖

org.apache.httpcomponents:httpclient org.apache.httpcomponents:HttpClient的

and configured Rest Template Request Factory 和配置的剩余模板请求工厂

    restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());

Apparently the default request factory SimpleClientHttpRequestFactory is not able to read the response body for 401 Http Status. 显然,默认请求工厂SimpleClientHttpRequestFactory无法读取401 Http Status的响应正文。

Why do you believe an HttpStatusCodeException must have a response body? 为什么您认为HttpStatusCodeException 必须具有响应主体? That is not the case. 事实并非如此。
Look at the constructor of RestClientResponseException , its direct superclass 看一下RestClientResponseException的构造RestClientResponseException ,它的直接超类

 ...

 * @param responseBody the response body content (may be {@code null})
 * @param responseCharset the response body charset (may be {@code null})
 */
public RestClientResponseException(
        String message, 
        int statusCode,
        String statusText,
        @Nullable HttpHeaders responseHeaders, 
        @Nullable byte[] responseBody, 
        @Nullable Charset responseCharset) { ... }

You need to expect that case, and provide a custom response body. 您需要预料到这种情况,并提供自定义响应正文。
The actual exception message is a good value to return as part of the body, which might be a complex object. 实际的异常消息是作为正文的一部分返回的一个好值,它可能是一个复杂的对象。

I was having a similar issue with a 400 response, and the above solution using the HttpComponentsClientHttpRequestFactory() did not work.我遇到了类似的 400 响应问题,上述使用HttpComponentsClientHttpRequestFactory()解决方案不起作用。 I was able to read the body by using restTemplate.setFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory));我能够通过使用restTemplate.setFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory));来读取正文restTemplate.setFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory)); instead.反而。 Not sure if anybody is having my same problem but hopefully this helps if the chosen solution doesn't work for you.不确定是否有人遇到我同样的问题,但如果所选的解决方案不适合您,希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM