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?
JsonMappingException.getPath()
return an empty array and does not help. The only information I can get isdetailMessage
string that I don't want to parse. – Ambagious