Issue
I am using spring boot 2.2.6 and Jackson 2.10.3 with Java 8. I am using localdatetime objects through out my project. Jackson is not able to parse LocalDateTime properly (or may be it's default format) and sending date in json response as in array format like below
"createdDate": [
2020,
8,
31,
0,
0,
0,
80000000
]
As described in JSON Java 8 LocalDateTime format in Spring Boot , Spring boot 2 has already default jackson-datatype-jsr310:2.10.3 on classpath. I want dates to represent in json as 2020-03-31:00 in whole project. First solution doesn't work in the above link. After that i have tried @JsonSerialize annotation and it works but i don't want to apply on each and every class. So also tried to override object mapper but it didn't work
@Primary
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
SimpleModule module = new SimpleModule("my custom date serializer");
module.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer());
mapper.registerModule(module);
return mapper;
}
Also tried to customize Jackson2ObjectMapperBuilder, but still have date in array format
@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
SimpleModule module = new SimpleModule("my custom date serializer");
module.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer());
builder.modulesToInstall(modules)
return builder;
}
Tried with also Jackson2ObjectMapperBuilderCustomizer
@Configuration
public class JacksonConfiguration {
@Primary
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> {
builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
};
}
}
Contoller
@RestConroller
@RequestMapping("/user")
UserController {
User getUser(){
User user = new User()
user.createdDate = LocalDateTime.now();
return user;
}
}
is there anything i can do at global level, so every date in the project will be serialized as in string format like 2020-09-01 ?
Any suggestion will be helpful.
Solution
The culprit was @EnableWebMVC
. One of my cors class was having @EnableWebMVC
annotation. This was disabling the spring boot auto configuration and also overriding the mapper confiiguration. This won't let you to override the configuration of object mapper. By removing @EnableMVC
annotation, this is working fine now. In SpringBoot2, we don't need to do any configuration explicitly related to date serialization, it will automatically parse the java 8 dates in "yyyy-mm-dd" format.
Answered By - Prabjot Singh
Answer Checked By - Terry (JavaFixing Volunteer)