Skip to content
Advertisement

How to restrict jackson from parsing millis to LocalDate in json request

I need to validate LocalDate fields in json requests. What i want is to prevent deserializing numbers as miilis to LocalDate. Here is example:

I have an entity:

JavaScript

Jackson2ObjectMapperBuilder config:

JavaScript

Now if i’m receiveing:

JavaScript

the result is birthDate=1970-01-02

I’m able to do so by setting leniency to false:

JavaScript

And then it’s working by throwing MismatchedInputException

But it’s a little brutal to backward compatibility of our service, because we need to change all our date patterns from “yyyy-MM-dd” to “uuuu-MM-dd” and i wonder is there some solution to say jackson “If you see numbers or anything different from the pattern while deserialization, throw an exception”

Advertisement

Answer

You could write a custom LocalDateDeserializer:

JavaScript

and register it when needed.

Here my simple Tests:

JavaScript

EDIT

Deserializer uses Pattern

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