From b89e62e5f6bf066952831c8515910ab08deb9d27 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Tue, 20 Jan 2015 14:57:14 +0100 Subject: [PATCH] Support specifying Jackson TimeZone and Locale Issue: SPR-12594 --- .../json/Jackson2ObjectMapperBuilder.java | 44 +++++++++++++++++++ .../json/Jackson2ObjectMapperFactoryBean.java | 30 +++++++++++++ .../Jackson2ObjectMapperBuilderTests.java | 34 ++++++++++++++ .../Jackson2ObjectMapperFactoryBeanTests.java | 44 +++++++++++++++++++ 4 files changed, 152 insertions(+) diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java b/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java index c48947a303..a8b0ebcf33 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java @@ -18,11 +18,14 @@ package org.springframework.http.converter.json; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.time.ZoneId; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; +import java.util.Locale; import java.util.Map; +import java.util.TimeZone; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonGenerator; @@ -75,6 +78,10 @@ public class Jackson2ObjectMapperBuilder { private DateFormat dateFormat; + private Locale locale; + + private TimeZone timeZone; + private AnnotationIntrospector annotationIntrospector; private PropertyNamingStrategy propertyNamingStrategy; @@ -134,6 +141,37 @@ public class Jackson2ObjectMapperBuilder { return this; } + /** + * Override the default {@link Locale} to use for formatting. + * Default value used is {@link Locale#getDefault()}. + * @since 4.1.5 + */ + public Jackson2ObjectMapperBuilder locale(Locale locale) { + this.locale = locale; + return this; + } + + /** + * Override the default {@link TimeZone} to use for formatting. + * Default value used is UTC (NOT local timezone). + * @since 4.1.5 + */ + public Jackson2ObjectMapperBuilder timeZone(TimeZone timeZone) { + this.timeZone = timeZone; + return this; + } + + /** + * Override the default {@link TimeZone} to use for formatting. + * Default value used is UTC (NOT local timezone). + * @param zoneId the time-zone ID + * @since 4.1.5 + */ + public Jackson2ObjectMapperBuilder timeZone(String zoneId) { + this.timeZone = TimeZone.getTimeZone(ZoneId.of(zoneId)); + return this; + } + /** * Set an {@link AnnotationIntrospector} for both serialization and deserialization. */ @@ -447,6 +485,12 @@ public class Jackson2ObjectMapperBuilder { if (this.dateFormat != null) { objectMapper.setDateFormat(this.dateFormat); } + if (this.locale != null) { + objectMapper.setLocale(this.locale); + } + if (this.timeZone != null) { + objectMapper.setTimeZone(this.timeZone); + } if (this.annotationIntrospector != null) { objectMapper.setAnnotationIntrospector(this.annotationIntrospector); diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java b/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java index bdca6e41bb..b6d7cb57ad 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java @@ -19,7 +19,9 @@ package org.springframework.http.converter.json; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.List; +import java.util.Locale; import java.util.Map; +import java.util.TimeZone; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.AnnotationIntrospector; @@ -171,6 +173,34 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean