I'm creating a custom Jackson deserializer class to map a JSON payload to an object. In my deserialize
method I did some checks on the JSON payload to see whether any fields are missing or incongruent to the POJO.
I tried throwing exceptions in the deserialize
method with something like this:
@Override
public MyObject deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
// if some fields are missing throw error:
throw new MissingFieldsException("name field is missing");
}
However, I'm not allowed to to throw my own exception in the deserialize
method because the method implements a interface which I can only throw IOException
and JsonProcessingException
:
public abstract T deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException;
In this case, how do I do validations of JSON payloads when doing deserialisations?