'Jackson accepting negative dates
I am trying to get a date field from JSON in a spring-boot application with Jackson. The JSONFormat looks like this:
@NotNull(message = ValidationErrors.NOT_BLANK_MESSAGE)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyyMMdd")
private Date date;
It works fine for most of the cases but when I pass 2017-0526, it is automatically converting it to 10th of May, 2018.
I want to throw exception in case the date is not in the yyyyMMdd format or contains minus sign. I tried going through stack overflow and Jackson documentation, but couldn't find anything.
Why is JsonFormat accepting negative dates?
Is there any workaround for this, so that it throws exception when such dates are passed?
Solution 1:[1]
I wanted something that will affect the whole (spring-boot) project and came up with this:
@Configuration
public class JsonConfiguration {
@Bean
public Jackson2ObjectMapperBuilderCustomizer customize() {
return builder -> builder
.dateFormat(StdDateFormat.instance.withLenient(false))
.build();
}
}
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 | Isaace |
