'How to output/serialize correct YearMonth value to display on form using Spring annotation?
I have the following variable in my POJO class being used to load the correct saved data into a form:
@JsonDeserialize(using=YearMonthDeserializer.class)
@JsonSerialize(using=YearMonthDeserializer.class)
private YearMonth date;
The correct saved value that should be loaded into form is "12/2010". However, it gets displayed as "02/0101". I believe it is because the default YearMonth format is "2010-12" and its not recognizing how to output it in the form field with the given input mask. I have tried adding the @JsonFormat(pattern="MM/yyyy") annotation as well as the @DateTimeFormat(pattern="MM/yyyy") but it doesn't seem to fix the issue. Any help is appreciated!
Solution 1:[1]
You can try this
@JsonSerialize(using = YearMonthSerializer.class)
@JsonDeserialize(using = YearMonthDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM/yyyy")
YearMonth date;
output {"date":"04/2022"}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |