Support specifying Jackson TimeZone and Locale
Issue: SPR-12594
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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<ObjectMapper
|
||||
this.builder.simpleDateFormat(format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the default {@link Locale} to use for formatting.
|
||||
* Default value used is {@link Locale#getDefault()}.
|
||||
* @since 4.1.5
|
||||
*/
|
||||
public void setLocale(Locale locale) {
|
||||
this.builder.locale(locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the default {@link TimeZone} to use for formatting.
|
||||
* Default value used is UTC (NOT local timezone).
|
||||
* @since 4.1.5
|
||||
*/
|
||||
public void setTimeZone(TimeZone timeZone) {
|
||||
this.builder.timeZone(timeZone);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 void setTimeZone(String zoneId) {
|
||||
this.builder.timeZone(zoneId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an {@link AnnotationIntrospector} for both serialization and deserialization.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user