So I've managed to figure out a solution, but if you have an alternative please post it.
I ended up creating a new primary ObjectMapper
bean, and registering a new module with a custom serializer for OffsetDateTime
. I'm able to set my own date format in here, using java.time.format.DateTimeFormatter
. I also had to register the 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;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…