处理 JAX-RS 2.0 客户机库中的定制错误响应
我开始在 JAX-RS 中使用新的客户端 API 库,并且到目前为止非常喜欢它。然而,我发现了一件我无法弄清楚的事情。我使用的 API 具有自定义错误消息格式,如下所示,例如:
{
"code": 400,
"message": "This is a message which describes why there was a code 400."
}
它返回 400 作为状态代码,但也包括一条描述性错误消息,告诉您做错了什么。
但是,JAX-RS 2.0 客户机正在将 400 状态重新映射到通用状态,并且我丢失了良好的错误消息。它正确地将其映射到 BadRequestException,但带有通用的“HTTP 400 错误请求”消息。
javax.ws.rs.BadRequestException: HTTP 400 Bad Request
at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:908)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:770)
at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:90)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:671)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:667)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:396)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:296)
是否有某种可以注入的拦截器或自定义错误处理程序,以便我可以访问真正的错误消息。我一直在查看文档,但看不到任何方法。
我现在正在使用泽西岛,但我使用CXF尝试了这个,并得到了相同的结果。下面是代码的外观。
Client client = ClientBuilder.newClient().register(JacksonFeature.class).register(GzipInterceptor.class);
WebTarget target = client.target("https://somesite.com").path("/api/test");
Invocation.Builder builder = target.request()
.header("some_header", value)
.accept(MediaType.APPLICATION_JSON_TYPE)
.acceptEncoding("gzip");
MyEntity entity = builder.get(MyEntity.class);
更新:
我实现了下面评论中列出的解决方案。它略有不同,因为在 JAX-RS 2.0 客户机 API 中,这些类发生了一些变化。我仍然认为默认行为是错误的,给出一个通用的错误消息并丢弃真实的错误消息。我理解为什么它不会解析我的错误对象,但应该返回未解析的版本。我最终得到了库已经完成的复制异常映射。
感谢您的帮助。
这是我的过滤器类:
@Provider
public class ErrorResponseFilter implements ClientResponseFilter {
private static ObjectMapper _MAPPER = new ObjectMapper();
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
// for non-200 response, deal with the custom error messages
if (responseContext.getStatus() != Response.Status.OK.getStatusCode()) {
if (responseContext.hasEntity()) {
// get the "real" error message
ErrorResponse error = _MAPPER.readValue(responseContext.getEntityStream(), ErrorResponse.class);
String message = error.getMessage();
Response.Status status = Response.Status.fromStatusCode(responseContext.getStatus());
WebApplicationException webAppException;
switch (status) {
case BAD_REQUEST:
webAppException = new BadRequestException(message);
break;
case UNAUTHORIZED:
webAppException = new NotAuthorizedException(message);
break;
case FORBIDDEN:
webAppException = new ForbiddenException(message);
break;
case NOT_FOUND:
webAppException = new NotFoundException(message);
break;
case METHOD_NOT_ALLOWED:
webAppException = new NotAllowedException(message);
break;
case NOT_ACCEPTABLE:
webAppException = new NotAcceptableException(message);
break;
case UNSUPPORTED_MEDIA_TYPE:
webAppException = new NotSupportedException(message);
break;
case INTERNAL_SERVER_ERROR:
webAppException = new InternalServerErrorException(message);
break;
case SERVICE_UNAVAILABLE:
webAppException = new ServiceUnavailableException(message);
break;
default:
webAppException = new WebApplicationException(message);
}
throw webAppException;
}
}
}
}