사용자 도구

사이트 도구


springframework:springboot:json

차이

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

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
springframework:springboot:json [2019/01/11 19:26]
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>
 {   {  
줄 166: 줄 170:
 <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); +
-            } +
-        }; +
     }     }
 } }
줄 211: 줄 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.1547202369.txt.gz · 마지막으로 수정됨: 2019/01/11 19:26 저자 kwon37xi