5

Jackson's JavaTimeModule serialize/deserializejava.time well globally, but its default date-time format is ISO standard, like 2018-01-10T10:20:30 for LocalDateTime and 2018-01-10T10:20:30+08:00 for OffsetDateTime. But I need to set a global local format like 2018-01-10 10:20:30 for LocalDateTime and OffsetDateTime, without T and OffsetTime (use local default OffsetTime). How can I do this?

Notes: I know about @JsonFormat, @JsonSerialize and @JsonDeserialize. That is not global setting.

RJ.Hwang
  • 1,683
  • 14
  • 24

3 Answers3

6

Spring boot

    @SpringBootApplication
    public class Application implements Jackson2ObjectMapperBuilderCustomizer {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }

        @Override
        public void customize(Jackson2ObjectMapperBuilder builder) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(formatter);

            builder.failOnEmptyBeans(false) // prevent InvalidDefinitionException Error
                    .serializerByType(LocalDateTime.class, localDateTimeSerializer);
        }
    }

Springboot & Spring Framework

    @Configuration
    public class WebMvcConfig implements WebMvcConfigurer {
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(formatter);
            LocalDateTimeDeserializer localDateTimeDeserializer = new LocalDateTimeDeserializer(formatter);

            JavaTimeModule module = new JavaTimeModule();
            module.addSerializer(LocalDateTime.class, localDateTimeSerializer);
            module.addDeserializer(LocalDateTime.class, localDateTimeDeserializer);

            ObjectMapper mapper = new ObjectMapper();
            mapper.registerModule(module);
            // add converter at the very front
            // if there are same type mappers in converters, setting in first mapper is used.
            converters.add(0, new MappingJackson2HttpMessageConverter(mapper));
        }
    }

hope this could help you.

coffeenjava
  • 106
  • 1
  • 6
2

If you are using a single instance of ObjectMapper globally, (and want a solution independent of Spring/Java 8 jackson modules,) you can do something like:

public ObjectMapper getCustomConfigMapper() {
    final ObjectMapper mapper = new ObjectMapper();
    final SimpleModule module = new SimpleModule();
    module.addDeserializer(LocalDateTime.class, new CustomLocaDateTimeDeserializer());
    mapper.registerModule(module);
    return mapper;
}

public static class CustomLocaDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {

    public static final DateTimeFormatter CUSTOM_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext __) throws IOException {
        final String value = jsonParser.getText().strip();
        return LocalDateTime.parse(value, CUSTOM_FORMATTER);
    }
}
aksh1618
  • 2,245
  • 18
  • 37
0

You can configure an ObjectMapper. This page explains how https://www.baeldung.com/jackson-serialize-dates. I think you want something close to example 4 on that page.

Then you need to make that the global ObjectMapper. Different frameworks use different methods. This page explains how to do that in Spring and Spring-boot Configuring ObjectMapper in Spring. For others just Google for it.

DCTID
  • 1,277
  • 9
  • 13
  • Error 403 when I open – RJ.Hwang Aug 24 '18 at 02:57
  • I know how to config ObjectMapper in SpringBoot, But that's not the global setting for java-time formatting. – RJ.Hwang Aug 24 '18 at 03:01
  • The link works fine for me, yours seems to have '>' at the end breaking it. The link has examples that can format a date without the T. I'm not sure what else you need. – DCTID Aug 24 '18 at 03:19
  • When open https://www.baeldung.com/jackson-serialize-dates, the page show `Your are not authorized to view this page`. – RJ.Hwang Aug 24 '18 at 06:34