Is there a solution that allows Jackson to continue to parse JSON after recognizing a structural problem with JSON that will cause JsonMappingExceptionit to collect all the problems and return them all with one exception to the composite mapping?
In the example below, there are two properties on the JSON input that do not match the class Person, but Jackson stops when meeting the first one.
It would be great to collect information on ALL structural issues for use on a client-side display.
class JacksonValidationHandlingSpec extends Specification {
@Canonical
static class Person {
String firstName
String lastName
}
@Test
void "Unknown fields"() {
setup:
ObjectMapper mapper = new ObjectMapper();
UnrecognizedPropertyException upe
try {
mapper.readValue("""
{"firstName":"john",
"lastName":"doe",
"middleName": "d",
"anotherUnknownField": "a"}
""", Person.class)
} catch(UnrecognizedPropertyException e) {
upe = e
}
expect:
upe != null
upe.getPath().get(0).getFieldName() == "middleName"
}
}
source
share