Jackson + Autovalue Builder bean验证

时间:2017-04-12 08:29:48

标签: spring java-8 jackson auto-value

我有以下Event DTO课程:

@AutoValue
@JsonDeserialize(builder = AutoValue_Event.Builder.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class Event {

    public static Event.Builder builder() {
        return new AutoValue_Event.Builder();
    }

    public abstract UUID id();

    @NotNull
    public abstract Type type();

    @NotNull
    @JsonSerialize(using = LocalDateSerializer.class)
    @JsonDeserialize(using = LocalDateDeserializer.class)
    public abstract LocalDate date();

    @AutoValue.Builder
    public abstract static class Builder {

        @JsonProperty("id")
        public abstract Builder id(UUID id);

        @JsonProperty("type")
        public abstract Builder type(Type type);

        @JsonProperty("date")
        public abstract Builder date(LocalDate date);

   }
}

验证适用于typedate属性,当有效负载不正确时,杰克逊会按预期投出JsonMappingException。不幸的是,返回的错误消息是text/plain,如: JsonMappingException: Can not construct instance of project.dto.AutoValue_Event$Builder, problem: Missing required properties: type

有没有办法处理这些验证错误并返回显式的json错误对象?

我发现this post捕获异常并返回一个显式的json,但是我无法映射反序列化失败的字段的自定义错误消息(我不想解析消息异常以了解哪个字段是不正确的)。任何的想法?

0 个答案:

没有答案