I'm trying to achieve something absurdly simple, that is set the global time format to all json serialized in the spring boot application... I've tried many suggestions from other questions but it looks like jackson chooses to ignore whatever configuration i set to object mapper, my code is
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm")));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm")));
mapper.registerModule(javaTimeModule);
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.configure(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE, false);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper;
}
if I autowire the ObjectMapper and use it manually like System.out.println(objectMapper.writeValueAsString(LocalDateTime.now()));
I get the dates in the format I want.
But all my controllers keep generating json in this format 2022-06-30T22:44:11
All my entities and Dtos use LocalDateTime
as time object...
What am I missing to make it work?
Please dont suggest annotate all my LocalDateTime to set the pattern, I want a configuration that I can set globally
thanks