사용자 도구

사이트 도구


springframework:springboot:json

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
springframework:springboot:json [2019/01/11 17:45]
kwon37xi
springframework:springboot:json [2020/11/23 19:00] (현재)
kwon37xi [WRITE_DATES_AS_TIMESTAMPS=false 사용]
줄 7: 줄 7:
  
 ==== 기본 설정 ==== ==== 기본 설정 ====
-  * 의존성에 다음 추가해야 JSR310 날짜/시간 지원됨.<code groovy>+  * SpringBoot 2.x 는 starter 추가 하면 Jackson ''ObjectMapper'' 자동 생성 설정<code groovy> 
 +api 'org.springframework.boot:spring-boot-starter-json' 
 +</code> 
 +  * 구버전 SpringBoot 에서는 의존성에 다음 추가해야 JSR310 날짜/시간 지원됨.<code groovy>
 compile('com.fasterxml.jackson.datatype:jackson-datatype-jsr310') compile('com.fasterxml.jackson.datatype:jackson-datatype-jsr310')
 compile('com.fasterxml.jackson.datatype:jackson-datatype-jdk8') compile('com.fasterxml.jackson.datatype:jackson-datatype-jdk8')
줄 42: 줄 45:
 spring.jackson.serialization.write-dates-as-timestamps: false spring.jackson.serialization.write-dates-as-timestamps: false
 </code> </code>
-  * SpringBoot 2.x 는 이값이 **기본 ''false''** 이로 ISO 포맷을 사용할 경우에는 ''simpleDateFormat''만 따로 ISO와 동일하게 설정해주면 된다.+  * SpringBoot 2.x 는 이값이 **기본 ''false''** 이고 Java 8 date/time 에 대해 자동으로 ISO 포맷을 사용하게 한다. ''simpleDateFormat''만 따로 ISO와 동일하게 설정해주면 된다
 +    * **''simpleDateFormat(), dateFormat()''을 설정하면 ''ObjectMapper''가 non-thread-safe 하게 돼 버린다.** 하지말고, ''java.util.Date''도 사용하지 말 것.
   * 이렇게 하면 ''java.util.Date'',''java.time.*'' 모두 ISO 혹은 그 유사 포맷으로 직렬화된다.<code>   * 이렇게 하면 ''java.util.Date'',''java.time.*'' 모두 ISO 혹은 그 유사 포맷으로 직렬화된다.<code>
 {   {  
줄 115: 줄 119:
 }</code> }</code>
   * ''DateTimeForamtter.ISO_ZONED_DATE_TIME''은 표준이 아니다. jackson은 ''ZonedDateTime''에 대해 기본으로 ''ISO_OFF_SET_DATE_TIME''을 사용한다.   * ''DateTimeForamtter.ISO_ZONED_DATE_TIME''은 표준이 아니다. jackson은 ''ZonedDateTime''에 대해 기본으로 ''ISO_OFF_SET_DATE_TIME''을 사용한다.
 +
 +==== Jackson2ObjectMapperBuilder 사용시 Module 추가 ====
 +  * ''Jackson2ObjectMapperBuilder'' 사용시 모듈을 추가하려면 ''modules'' 메소드 혹은 ''modulesToInstall''를 사용한다.
 +  * ''modules(...)'' : Spring과 Jackson default 모듈 탐색을 모두 취소하고 오직 이 메소드 인자로 전달된 모듈만 탑재
 +  * ''modulesToInstall(...)'' : Spring(JSR-310 or jodatime 등 자동추가)과 Jackson default 모듈 탑재를 수행하고 그 뒤에 명시된 모듈을 추가 탑재
 ==== @EnableWebMvc ==== ==== @EnableWebMvc ====
 ''@EnableWebMvc''와 ''WebMvcConfigurerAdapter''를 사용하는 순간 더이상 SpringBoot가 아니고 Spring 이기 때문에 위의 설정이 먹지 않게 된다. [[springframework:springboot:mvc|SpringBoot and Spring MVC]] 참조 [[https://github.com/spring-projects/spring-boot/issues/2116|Spring Boot Issue 2116]] ''@EnableWebMvc''와 ''WebMvcConfigurerAdapter''를 사용하는 순간 더이상 SpringBoot가 아니고 Spring 이기 때문에 위의 설정이 먹지 않게 된다. [[springframework:springboot:mvc|SpringBoot and Spring MVC]] 참조 [[https://github.com/spring-projects/spring-boot/issues/2116|Spring Boot Issue 2116]]
줄 156: 줄 165:
 ==== WebFlux Jackson 설정 ==== ==== WebFlux Jackson 설정 ====
   * [[springframework:webflux|Spring WebFlux]] Jackson 설정   * [[springframework:webflux|Spring WebFlux]] Jackson 설정
 +  * ''@EnableWebFlux''는 SpringBoot에서는 해서는 안 된다.
   * https://stackoverflow.com/a/43241547/1051402   * https://stackoverflow.com/a/43241547/1051402
  
 <code java> <code java>
 @Configuration @Configuration
-public class Config { +public class Config implements WebFluxConfigurer, Jackson2ObjectMapperBuilderCustomizer 
- +    @Override 
-    @Bean +    public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) { 
-    JavaTimeModule javatimeModule(){ +        jacksonObjectMapperBuilder 
-        return new JavaTimeModule(); +                .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) 
-    } +                .timeZone(TimeZone.getDefault()) 
- +                .locale(Locale.getDefault()) 
-    @Bean +                .simpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
-    Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(){ +
-    return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.featuresToEnable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) +
-            .mixIn(MyClass.class, MyClassMixin.class); +
-    } +
- +
- +
-    @Bean +
-    Jackson2JsonEncoder jackson2JsonEncoder(ObjectMapper mapper)+
-       return new Jackson2JsonEncoder(mapper)+
-    } +
- +
-    @Bean +
-    Jackson2JsonDecoder jackson2JsonDecoder(ObjectMapper mapper){ +
-        return new Jackson2JsonDecoder(mapper); +
-    } +
- +
-    @Bean +
-    WebFluxConfigurer webFluxConfigurer(Jackson2JsonEncoder encoder, Jackson2JsonDecoder decoder){ +
-        return new WebFluxConfigurer() { +
-            @Override +
-            public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) { +
-                configurer.defaultCodecs().jackson2Encoder(encoder); +
-                configurer.defaultCodecs().jackson2Decoder(decoder); +
-            } +
-        }; +
     }     }
 +}
 </code> </code>
  
줄 204: 줄 189:
   * [[https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-customize-the-jackson-objectmapper|How to customize the Jackson ObjectMapper]]   * [[https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-customize-the-jackson-objectmapper|How to customize the Jackson ObjectMapper]]
   * [[https://dzone.com/articles/spring-web-service-response-filtering|Spring Web Service Response Filtering - DZone Java]] - 응답 Json Filter   * [[https://dzone.com/articles/spring-web-service-response-filtering|Spring Web Service Response Filtering - DZone Java]] - 응답 Json Filter
 +  * [[https://www.baeldung.com/spring-boot-formatting-json-dates|Formatting JSON Dates in Spring Boot | Baeldung]]
springframework/springboot/json.1547196306.txt.gz · 마지막으로 수정됨: 2019/01/11 17:45 저자 kwon37xi