1

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

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 类并将其注册到 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();
}
}

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

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

@JsonIgnoreProperties(value = "stackTrace")

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

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

4

1 回答 1

1

我认为更简单的选择是将异常转换为Errorbean 并将其返回,如下所示。

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;
    }
}
于 2017-04-06T06:34:27.757 回答