![](/img/trans.png)
[英]JAXRS/Jersey 2 - Validation errors does not invoke the ExceptionMapper
[英]Use ExceptionMapper to log CLIENT_ERRORs
我正在使用使用Dropwizard的應用程序,該應用程序具有ExceptionMapper的以下實現: https : //github.com/dropwizard/dropwizard/blob/master/dropwizard-jersey/src/main/java/io/dropwizard/jersey/errors /LoggingExceptionMapper.java
這種實現方式的問題在於,即使它捕獲了4 **和5 **錯誤,也只記錄了5 **錯誤。
我需要實現ExceptionMapper以便根本不使用LoggingExceptionMapper,而我的CustomExceptionMapper會記錄CLIENT_ERRORs和SERVER_ERRORs。
我想知道我的應用程序如何知道它需要使用CustomExceptionMapper而不是Dropwizard類?
另外,如果滿足條件,可以添加CLIENT_ERROR來注銷所有錯誤?
@Override
public Response toResponse(E exception) {
// If we're dealing with a web exception, we can service certain types of request (like
// redirection or server errors) better and also propagate properties of the inner response.
if (exception instanceof WebApplicationException) {
final Response response = ((WebApplicationException) exception).getResponse();
Response.Status.Family family = response.getStatusInfo().getFamily();
if (family.equals(Response.Status.Family.REDIRECTION)) {
return response;
}
if (family.equals(Response.Status.Family.SERVER_ERROR) || family.equals(Response.Status.Family.CLIENT_ERROR) {
logException(exception);
}
return Response.fromResponse(response)
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(new ErrorMessage(response.getStatus(), exception.getLocalizedMessage()))
.build();
}
還是會有更好的方法來做到這一點?
關於ExceptionMapper的JAX-RS規范 :
當選擇異常映射提供程序來映射異常時,實現必須使用其泛型類型為異常的最接近超類的提供程序。
我的應用程序如何知道它需要使用CustomExceptionMapper而不是Dropwizard類?
您可以從應用程序中引發自定義異常,並為該特定異常創建ExceptionMapper。
如果滿足條件,將CLIENT_ERROR添加到注銷所有錯誤是否足夠?
是的,4xx和5xx系列具有所有錯誤響應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.