문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 다음 판 양쪽 다음 판 | ||
springframework:springboot:json [2018/07/18 16:57] kwon37xi [@EnableWebMvc] |
springframework:springboot:json [2020/07/10 14:29] kwon37xi [WRITE_DATES_AS_TIMESTAMPS=false 사용] |
||
---|---|---|---|
줄 37: | 줄 37: | ||
} | } | ||
</ | </ | ||
+ | |||
==== 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 는 이값이 **기본 '' | ||
* 이렇게 하면 '' | * 이렇게 하면 '' | ||
{ | { | ||
줄 52: | 줄 54: | ||
</ | </ | ||
* 그러나 '' | * 그러나 '' | ||
+ | * Formatter와 Jackson Serializers/ | ||
==== 모든 타입에 대해 일관성 있는 커스텀 형식 지원 ==== | ==== 모든 타입에 대해 일관성 있는 커스텀 형식 지원 ==== | ||
줄 62: | 줄 65: | ||
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() { | ||
줄 86: | 줄 95: | ||
} | } | ||
} | } | ||
+ | // 불필요 | ||
public static class CustomZonedDateTimeSerializer extends ZonedDateTimeSerializer { | public static class CustomZonedDateTimeSerializer extends ZonedDateTimeSerializer { | ||
public CustomZonedDateTimeSerializer() { | public CustomZonedDateTimeSerializer() { | ||
줄 103: | 줄 112: | ||
" | " | ||
" | " | ||
- | " | + | " |
}</ | }</ | ||
+ | * '' | ||
+ | ==== Jackson2ObjectMapperBuilder 사용시 Module 추가 ==== | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
==== @EnableWebMvc ==== | ==== @EnableWebMvc ==== | ||
- | '' | + | '' |
이때는 '' | 이때는 '' | ||
<code java> | <code java> | ||
줄 122: | 줄 137: | ||
public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { | public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { | ||
| | ||
- | | + | |
- | | + | |
- | | + | |
| | ||
| | ||
줄 138: | 줄 153: | ||
} | } | ||
// 파라미터를 ? | // 파라미터를 ? | ||
+ | </ | ||
+ | |||
+ | ==== @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(" | ||
+ | } | ||
+ | } | ||
</ | </ | ||
줄 146: | 줄 184: | ||
* [[http:// | * [[http:// | ||
* [[https:// | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// |