![](/img/trans.png)
[英]How to send the same response body as "ResponseStatusException" in a custom "ResponseEntityExceptionHandler" in Spring?
[英]ResponseEntityExceptionHandler returns empty response body for 401 exceptions
我正在尝试使用 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();
}
目前我的 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);
}
}
但是,仅对于 HttpStatus 401 异常,我得到一个空的响应正文。
日志示例:
400| {"error_description":"Bad client credentials","error":"invalid_client"}
401| Bad authentication response body | Response Body |
有谁知道我如何获得 401 异常的响应正文?
编辑:当使用邮递员调用身份验证服务器时,我得到非空 401 responseBody:
{
"error": "invalid_token",
"error_description": "Token was not recognised"
}
我终于能够记录响应正文的“ 401异常”。 我添加了依赖
org.apache.httpcomponents:HttpClient的
和配置的剩余模板请求工厂
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
显然,默认请求工厂SimpleClientHttpRequestFactory无法读取401 Http Status的响应正文。
为什么您认为HttpStatusCodeException
必须具有响应主体? 事实并非如此。
看一下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) { ... }
您需要预料到这种情况,并提供自定义响应正文。
实际的异常消息是作为正文的一部分返回的一个好值,它可能是一个复杂的对象。
我遇到了类似的 400 响应问题,上述使用HttpComponentsClientHttpRequestFactory()
解决方案不起作用。 我能够通过使用restTemplate.setFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory));
来读取正文restTemplate.setFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory));
反而。 不确定是否有人遇到我同样的问题,但如果所选的解决方案不适合您,希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.