Check Jackson + Autovalue Builder bean

I have the following EventDTO class :

@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);

   }
}

Validation works well for attributes typeand date, and Jackson throws JsonMappingExceptionas expected when the payload is incorrect. Unfortunately, the returned error message - it's text/plainlike: JsonMappingException: Can not construct instance of project.dto.AutoValue_Event$Builder, problem: Missing required properties: type.

Is there a way to handle these validation errors and return an explicit json error object?

I found this post to catch the exception and return an explicit json, but I cannot match the custom error message in which field deserialization failed (I don't know) t want to parse the message exception to know which field is wrong). Any ideas?

+6

Source: https://habr.com/ru/post/1016487/


All Articles