Skip to content
Advertisement

How to catch a @JsonFormat exception in spring and handle it gracefully to process the payload?

I have a spring app in which I am using the @JsonFormat annotation to deserialize a date format. But when I sent an array of elements my entire payload fails even if one of the entries have an invalid date.

Is there a way I can surpass this by gracefully handling this exception, by either replacing the failed date with a default value or ignoring that array entry.

jackson.version: 2.7.5, spring.version: 5.0.0.RELEASE

JavaScript

Advertisement

Answer

You could write a custom deserializer for your class where you set a default value in case something goes wrong. Something like:

JavaScript

Then on your class you could do something like:

JavaScript

You could also add @JsonIgnoreProperties(ignoreUnknown = true) over your class if you know that its value is not necessary always.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement