我有以下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);
}
}
验证适用于type
和属性,当有效负载不正确时date
,Jackson 会按预期抛出一个。JsonMappingException
不幸的是,返回的错误消息text/plain
类似于:
JsonMappingException: Can not construct instance of project.dto.AutoValue_Event$Builder, problem: Missing required properties: type
。
有没有办法处理这些验证错误并返回一个明确的 json 错误对象?
我发现这篇文章 可以捕获异常并返回显式 json,但我无法在反序列化失败的字段上映射自定义错误消息(我不想解析消息异常以了解哪个字段不正确)。任何想法?