【发布时间】:2015-12-19 22:23:56
【问题描述】:
我在 Wildfly 8.2.0.Final 中使用带有 RestEasy 的 Bean 验证:
@Path("/user")
@Produces(MediaType.APPLICATION_JSON)
public class UserEndpoint
{
//more code
@GET
@Path("/encrypt/{email}")
public Response fetchEncryptedId(@PathParam("email") @NotNull String email)
{
String encryptedUserId = userService.getEncryptedUserId(email);
return Response.ok().entity(new UserBo(encryptedUserId)).build();
}
}
这基本上有效。现在我想以 JSON 对象的形式获取响应,但我无法让它工作。我所有的“应用程序”异常都由我的异常映射器处理,这很有效:
@Provider
public class DefaultExceptionMapper implements ExceptionMapper<Exception>
{
private static final String MEDIA_TYPE = "application/json";
private LoggingService loggingService;
@EJB
public void setLoggingService(LoggingService loggingService)
{
this.loggingService = loggingService;
}
@Override
public Response toResponse(Exception exception)
{
ResponseObject responseObject = new ResponseObject();
responseObject.registerExceptionMessage(exception.getMessage());
if (exception instanceof ForbiddenException)
{
loggingService.log(LogLevel.ERROR, ((ForbiddenException)exception).getUserId(), ExceptionToStringMapper.map(exception));
return Response.status(Status.FORBIDDEN).type(MEDIA_TYPE).entity(responseObject).build();
}
//more handling
loggingService.log(LogLevel.ERROR, "", ExceptionToStringMapper.map(exception));
return Response.status(Status.INTERNAL_SERVER_ERROR).type(MEDIA_TYPE).entity(responseObject).build();
}
}
但 bean 验证以某种方式绕过它。然后我考虑使用 Throwable 而不是 Exception 但它也没有帮助。我猜 ExceptionMapper 没有被触发,因为 JAX-RS 和 JSR303 存在一些生命周期问题。但是我怎样才能同步它们来处理 bean 验证异常呢?
附加信息:异常通过了javax.ws.rs.container.ContainerResponseFilter,所以我可以通过在子类中实现过滤器方法来编写一些解决方法,但这不是干净的解决方案。目标是处理异常映射器中的异常。
【问题讨论】:
-
我认为异常映射器是要走的路。另见stackoverflow.com/questions/10516516/…。不知道为什么这在这种情况下不起作用。您是否尝试过按照其他帖子的建议注册专用映射器?
-
@Hardy 我也不是,这就是我问的原因。帖子中的映射器与我的相同,因此我的更宽容,因为我基本上捕捉到任何异常,而不仅仅是 ConstraintViolationException。 (我在调试器中,但它没有被调用)
标签: json wildfly resteasy bean-validation wildfly-8