<dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.15.2</version> </dependency>
SimpleModule を使う場合、
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
SimpleModule simpleModule = new SimpleModule(); simpleModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"))); simpleModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")) ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(simpleModule);
JavaTimeModule は、SimpleModule の継承
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
JavaTimeModule jtm = new JavaTimeModule(); jtm.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"))); jtm.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")) ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(jtm);
JavaTimeModule は、内部で以下がシリアライザ、デシリアライザが登録されている。
ことから、こちらを使った方が良いかも。。。
// // Instant variants: addDeserializer(Instant.class, InstantDeserializer.INSTANT); addDeserializer(OffsetDateTime.class, InstantDeserializer.OFFSET_DATE_TIME); addDeserializer(ZonedDateTime.class, InstantDeserializer.ZONED_DATE_TIME); // // Other deserializers addDeserializer(Duration.class, DurationDeserializer.INSTANCE); addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE); addDeserializer(LocalDate.class, LocalDateDeserializer.INSTANCE); addDeserializer(LocalTime.class, LocalTimeDeserializer.INSTANCE); addDeserializer(MonthDay.class, MonthDayDeserializer.INSTANCE); addDeserializer(OffsetTime.class, OffsetTimeDeserializer.INSTANCE); addDeserializer(Period.class, JSR310StringParsableDeserializer.PERIOD); addDeserializer(Year.class, YearDeserializer.INSTANCE); addDeserializer(YearMonth.class, YearMonthDeserializer.INSTANCE); addDeserializer(ZoneId.class, JSR310StringParsableDeserializer.ZONE_ID); addDeserializer(ZoneOffset.class, JSR310StringParsableDeserializer.ZONE_OFFSET); // then serializers: addSerializer(Duration.class, DurationSerializer.INSTANCE); addSerializer(Instant.class, InstantSerializer.INSTANCE); addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE); addSerializer(LocalDate.class, LocalDateSerializer.INSTANCE); addSerializer(LocalTime.class, LocalTimeSerializer.INSTANCE); addSerializer(MonthDay.class, MonthDaySerializer.INSTANCE); addSerializer(OffsetDateTime.class, OffsetDateTimeSerializer.INSTANCE); addSerializer(OffsetTime.class, OffsetTimeSerializer.INSTANCE); addSerializer(Period.class, new ToStringSerializer(Period.class)); addSerializer(Year.class, YearSerializer.INSTANCE); addSerializer(YearMonth.class, YearMonthSerializer.INSTANCE); /* 27-Jun-2015, tatu: This is the real difference from the old * {@link JSR310Module}: default is to produce ISO-8601 compatible * serialization with timezone offset only, not timezone id. * But this is configurable. */ addSerializer(ZonedDateTime.class, ZonedDateTimeSerializer.INSTANCE); // since 2.11: need to override Type Id handling // (actual concrete type is `ZoneRegion`, but that's not visible) addSerializer(ZoneId.class, new ZoneIdSerializer()); addSerializer(ZoneOffset.class, new ToStringSerializer(ZoneOffset.class)); // key serializers addKeySerializer(ZonedDateTime.class, ZonedDateTimeKeySerializer.INSTANCE); // key deserializers addKeyDeserializer(Duration.class, DurationKeyDeserializer.INSTANCE); addKeyDeserializer(Instant.class, InstantKeyDeserializer.INSTANCE); addKeyDeserializer(LocalDateTime.class, LocalDateTimeKeyDeserializer.INSTANCE); addKeyDeserializer(LocalDate.class, LocalDateKeyDeserializer.INSTANCE); addKeyDeserializer(LocalTime.class, LocalTimeKeyDeserializer.INSTANCE); addKeyDeserializer(MonthDay.class, MonthDayKeyDeserializer.INSTANCE); addKeyDeserializer(OffsetDateTime.class, OffsetDateTimeKeyDeserializer.INSTANCE); addKeyDeserializer(OffsetTime.class, OffsetTimeKeyDeserializer.INSTANCE); addKeyDeserializer(Period.class, PeriodKeyDeserializer.INSTANCE); addKeyDeserializer(Year.class, YearKeyDeserializer.INSTANCE); addKeyDeserializer(YearMonth.class, YearMonthKeyDeserializer.INSTANCE); addKeyDeserializer(ZonedDateTime.class, ZonedDateTimeKeyDeserializer.INSTANCE); addKeyDeserializer(ZoneId.class, ZoneIdKeyDeserializer.INSTANCE); addKeyDeserializer(ZoneOffset.class, ZoneOffsetKeyDeserializer.INSTANCE);