So, I managed to find a solution, but if you have an alternative, submit it.
As a result, I created a new primary ObjectMapper
bean and registered a new module using a special serializer for OffsetDateTime
. Here I can set my date format using java.time.format.DateTimeFormatter
. I also had to register a JavaTimeModule
with my mapper.
@Configuration public class JacksonOffsetDateTimeMapper{ @Primary @Bean public ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(new JavaTimeModule()); SimpleModule simpleModule = new SimpleModule(); simpleModule.addSerializer(OffsetDateTime.class, new JsonSerializer<OffsetDateTime>() { @Override public void serialize(OffsetDateTime offsetDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { jsonGenerator.writeString(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(offsetDateTime)); } }); objectMapper.registerModule(simpleModule); return objectMapper; } }
source share