7

I have a the following Event DTO 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);

   }
}

The validation works well for type and date attributes and Jackson throws a JsonMappingException as expected when the payload is not correct. Unfortunately, the returned error message is a text/plain like: JsonMappingException: Can not construct instance of project.dto.AutoValue_Event$Builder, problem: Missing required properties: type.

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

I found this post to catch the exception and return an explicit json but I can't map a custom error message on which field the deserialization failed (I don't want to parse the message exception to know which field is incorrect). Any idea?

Community
  • 1
  • 1
louis amoros
  • 2,418
  • 3
  • 19
  • 40
  • com.fasterxml.jackson.databind.JsonMappingException.getPath() - does this help. Reference has field information – user871199 Apr 13 '17 at 23:50
  • `JsonMappingException.getPath(‌​)` return an empty array and does not help. The only information I can get is `detailMessage` string that I don't want to parse. – louis amoros Apr 14 '17 at 07:12

0 Answers0