How to configure jackson with spring globally?

前端 未结 4 377
走了就别回头了
走了就别回头了 2021-01-20 12:47

To serialize deserialize object I am useing Jackson as flow

@JsonSerialize(using = LocalDateSerializer.class)
@JsonDeserialize(using = LocalDateDeserializer.         


        
4条回答
  •  失恋的感觉
    2021-01-20 13:25

    Well you can install modules for using things like java datetime or jodatime. checkout this:

        @Bean
        Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
            JavaTimeModule module = new JavaTimeModule();
            module.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE);
            module.addSerializer(ZonedDateTime.class, JSR310DateTimeSerializer.INSTANCE);
            module.addSerializer(LocalDateTime.class, JSR310DateTimeSerializer.INSTANCE);
            module.addSerializer(Instant.class, JSR310DateTimeSerializer.INSTANCE);
            module.addDeserializer(LocalDate.class, JSR310LocalDateDeserializer.INSTANCE);
            return new Jackson2ObjectMapperBuilder()
                    .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                    .findModulesViaServiceLoader(true)
                    .modulesToInstall(module);
        }
    

提交回复
热议问题