문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
|
springframework:springboot:json [2018/11/27 14:27] kwon37xi [WRITE_DATES_AS_TIMESTAMPS=false 사용] |
springframework:springboot:json [2020/11/23 19:00] (현재) kwon37xi [WRITE_DATES_AS_TIMESTAMPS=false 사용] |
||
|---|---|---|---|
| 줄 7: | 줄 7: | ||
| ==== 기본 설정 ==== | ==== 기본 설정 ==== | ||
| - | * 의존성에 다음 추가해야 JSR310 날짜/ | + | * SpringBoot 2.x 는 starter 추가 하면 Jackson '' |
| + | api ' | ||
| + | </ | ||
| + | * 구버전 SpringBoot 에서는 | ||
| compile(' | compile(' | ||
| compile(' | compile(' | ||
| 줄 37: | 줄 40: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| ==== WRITE_DATES_AS_TIMESTAMPS=false 사용 ==== | ==== WRITE_DATES_AS_TIMESTAMPS=false 사용 ==== | ||
| * 제일 좋은 것은 처음부터 '' | * 제일 좋은 것은 처음부터 '' | ||
| spring.jackson.serialization.write-dates-as-timestamps: | spring.jackson.serialization.write-dates-as-timestamps: | ||
| </ | </ | ||
| - | * SpringBoot 2.x 는 이값이 **기본 '' | + | * SpringBoot 2.x 는 이값이 **기본 '' |
| + | * **'' | ||
| * 이렇게 하면 '' | * 이렇게 하면 '' | ||
| { | { | ||
| 줄 53: | 줄 58: | ||
| </ | </ | ||
| * 그러나 '' | * 그러나 '' | ||
| + | * Formatter와 Jackson Serializers/ | ||
| ==== 모든 타입에 대해 일관성 있는 커스텀 형식 지원 ==== | ==== 모든 타입에 대해 일관성 있는 커스텀 형식 지원 ==== | ||
| 줄 63: | 줄 69: | ||
| SpringApplication.run(DemoApplication.class, | SpringApplication.run(DemoApplication.class, | ||
| } | } | ||
| + | |||
| + | // TODO : 아래 예에서는 deserializer가 빠져있다!! deserializer도 추가해줘야 일관성있게 된다. | ||
| + | // ISO 포맷을 사용할경우 SpringBoot 2 에서 기본적으로 ISO Format으로 세팅하기 때문에 굳이 복잡하게 설정할 필요없이 | ||
| + | // java.util.Date 에 대해서만 일관성있게 설정한다. | ||
| @Override | @Override | ||
| public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) { | public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) { | ||
| LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ISO_LOCAL_DATE_TIME); | LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ISO_LOCAL_DATE_TIME); | ||
| - | | + | // |
| - | LocalTimeSerializer localTimeSerializer = new LocalTimeSerializer(DateTimeFormatter.ISO_LOCAL_TIME); | + | // |
| - | | + | // |
| - | ZonedDateTimeSerializer zonedDateTimeSerializer = new CustomZonedDateTimeSerializer(); | + | // |
| jacksonObjectMapperBuilder | jacksonObjectMapperBuilder | ||
| - | .simpleDateFormat(" | + | |
| - | .serializerByType(LocalDateTime.class, | + | .locale(Locale.getDefault()) |
| - | .serializerByType(LocalDate.class, | + | |
| - | .serializerByType(LocalTime.class, | + | // |
| - | .serializerByType(OffsetDateTime.class, | + | // |
| - | .serializerByType(ZonedDateTime.class, | + | // |
| + | // | ||
| + | // | ||
| } | } | ||
| + | // 불필요 | ||
| public static class CustomOffsetDateTimeSerializer extends OffsetDateTimeSerializer { | public static class CustomOffsetDateTimeSerializer extends OffsetDateTimeSerializer { | ||
| protected CustomOffsetDateTimeSerializer() { | protected CustomOffsetDateTimeSerializer() { | ||
| 줄 87: | 줄 99: | ||
| } | } | ||
| } | } | ||
| + | // 불필요 | ||
| public static class CustomZonedDateTimeSerializer extends ZonedDateTimeSerializer { | public static class CustomZonedDateTimeSerializer extends ZonedDateTimeSerializer { | ||
| public CustomZonedDateTimeSerializer() { | public CustomZonedDateTimeSerializer() { | ||
| 줄 104: | 줄 116: | ||
| " | " | ||
| " | " | ||
| - | " | + | " |
| }</ | }</ | ||
| - | * TODO : **위 예에서는 deserializer가 빠져있다!!** deserializer도 | + | * '' |
| + | |||
| + | ==== Jackson2ObjectMapperBuilder 사용시 Module 추가 ==== | ||
| + | * '' | ||
| + | | ||
| + | | ||
| ==== @EnableWebMvc ==== | ==== @EnableWebMvc ==== | ||
| '' | '' | ||
| 줄 140: | 줄 157: | ||
| } | } | ||
| // 파라미터를 ? | // 파라미터를 ? | ||
| + | </ | ||
| + | |||
| + | ==== @JsonComponent ==== | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| + | ==== WebFlux Jackson 설정 ==== | ||
| + | * [[springframework: | ||
| + | * '' | ||
| + | * https:// | ||
| + | |||
| + | <code java> | ||
| + | @Configuration | ||
| + | public class Config implements WebFluxConfigurer, | ||
| + | @Override | ||
| + | public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) { | ||
| + | jacksonObjectMapperBuilder | ||
| + | .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) | ||
| + | .timeZone(TimeZone.getDefault()) | ||
| + | .locale(Locale.getDefault()) | ||
| + | .simpleDateFormat(" | ||
| + | } | ||
| + | } | ||
| </ | </ | ||
| 줄 148: | 줄 188: | ||
| * [[http:// | * [[http:// | ||
| * [[https:// | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||