Dropwizard 将自定义异常反序列化为 JSON?

Dropwizard deserialising custom exception as JSON?

我在扩展 BadRequestException 的 Dropwizard 服务中创建了一个新异常 class。

public class CustomBadRequestException extends BadRequestException {

    private static final long serialVersionUID = 1L;

    private List<ValidationFailureDto> validationFailures;

    public CustomBadRequestException() {
        super();
    }

    public CustomBadRequestException(final List<ValidationFailureDto> validationFailures) {
        super();
        this.validationFailures = validationFailures;
    }

    @ApiModelProperty(value = "List of validationFailures")
    public List<ValidationFailureDto> getValidationFailures() {
        return validationFailures;
    }
}

当我一开始抛出该异常时,我只取回反序列化的 BadRequestException,减去额外的 属性 (validationFailures)

{
code: "400",
message: "Bad request"
}

这是因为 Dropwizard 的内部有一个默认的异常映射器,允许 Jetty/Jackson 了解域异常以及如何发送适当的 HTTP 响应。

要克服这个问题,您可以实现自己的 ExceptionMapper class 并在 Dropwizard 中注册它。

public class CustomBadRequestExceptionMapper implements ExceptionMapper<SamplePackOrderBadRequestException> {

/**
 * Allows jackson to deserialise custom exceptions and its properties to JSON response
 *
 * @param exception exception
 * @return response object
 */
@Override
public Response toResponse(final SamplePackOrderBadRequestException exception) {

    if (exception instanceof SamplePackOrderBadRequestException) {

        SamplePackOrderBadRequestException samplePackOrderBadRequestException
                = (SamplePackOrderBadRequestException) exception;
        return Response
                .status(400)
                .entity(samplePackOrderBadRequestException)
                .build();
    }
    return Response.status(400).build();
}
}

然而,这个问题是它反序列化了 super (Throwable),所以你在响应中添加了我不想要的每个继承 属性。

为了解决这个问题,我尝试像这样添加 Jackson 注释:

@JsonIgnoreProperties(value = "stackTrace")

这不是最佳解决方案,因为除了 stackTrace 之外还有几个属性我需要忽略。

所以总而言之,我怎样才能让 Dropwizard 正确反序列化我的 CustomException class 而没有我不需要的所有额外混乱?

我认为更简单的选择是将异常转换为 Error bean 并 return 如下所示。

public class CustomBadRequestExceptionMapper implements ExceptionMapper<SamplePackOrderBadRequestException> {


@Override
public Response toResponse(final SamplePackOrderBadRequestException exception) {

    if (exception instanceof SamplePackOrderBadRequestException) {

        SamplePackOrderBadRequestException ex
            = (SamplePackOrderBadRequestException) exception;
         return Response
            .status(400)
            .entity(new ErrorBean(400,ex.getMessage,ex.getgetValidationFailures()))
            .build();
    }
    return Response.status(400).build();
}
}

和ErrorBean.java

    public static class ErrorBean{
    private int code;
    private String message;
    private List<ValidationFailureDto> failures;
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public List<ValidationFailureDto> getFailures() {
        return failures;
    }
    public void setFailures(List<ValidationFailureDto> failures) {
        this.failures = failures;
    }
}