Allow Jackson’s time zone to be configured via the environment
This commit adds a new property, spring.jackson.time-zone, that can be used to configure the time zone that Jackson uses when configuring dates. It affects the serialisation of both JDK and Joda date types. Closes gh-3505
This commit is contained in:
@@ -151,6 +151,9 @@ public class JacksonAutoConfiguration {
|
||||
builder.serializationInclusion(this.jacksonProperties
|
||||
.getSerializationInclusion());
|
||||
}
|
||||
if (this.jacksonProperties.getTimeZone() != null) {
|
||||
builder.timeZone(this.jacksonProperties.getTimeZone());
|
||||
}
|
||||
configureFeatures(builder, this.jacksonProperties.getDeserialization());
|
||||
configureFeatures(builder, this.jacksonProperties.getSerialization());
|
||||
configureFeatures(builder, this.jacksonProperties.getMapper());
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.jackson;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@@ -88,6 +89,12 @@ public class JacksonProperties {
|
||||
*/
|
||||
private JsonInclude.Include serializationInclusion;
|
||||
|
||||
/**
|
||||
* Time zone used when formatting dates. Configured using any recognized time zone
|
||||
* identifier, for example "America/Los_Angeles" or "GMT+10".
|
||||
*/
|
||||
private TimeZone timeZone = null;
|
||||
|
||||
public String getDateFormat() {
|
||||
return this.dateFormat;
|
||||
}
|
||||
@@ -140,4 +147,11 @@ public class JacksonProperties {
|
||||
this.serializationInclusion = serializationInclusion;
|
||||
}
|
||||
|
||||
public TimeZone getTimeZone() {
|
||||
return this.timeZone;
|
||||
}
|
||||
|
||||
public void setTimeZone(TimeZone timeZone) {
|
||||
this.timeZone = timeZone;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -374,6 +374,34 @@ public class JacksonAutoConfigurationTests {
|
||||
is(JsonInclude.Include.NON_NULL));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTimeZoneFormattingADateTime() throws JsonProcessingException {
|
||||
this.context.register(JacksonAutoConfiguration.class);
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.jackson.time-zone:America/Los_Angeles");
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.jackson.date-format:zzzz");
|
||||
this.context.refresh();
|
||||
ObjectMapper objectMapper = this.context.getBean(
|
||||
Jackson2ObjectMapperBuilder.class).build();
|
||||
DateTime dateTime = new DateTime(1436966242231L, DateTimeZone.UTC);
|
||||
assertEquals("\"Pacific Daylight Time\"",
|
||||
objectMapper.writeValueAsString(dateTime));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTimeZoneFormattingADate() throws JsonProcessingException {
|
||||
this.context.register(JacksonAutoConfiguration.class);
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.jackson.time-zone:GMT+10");
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "spring.jackson.date-format:z");
|
||||
this.context.refresh();
|
||||
ObjectMapper objectMapper = this.context.getBean(
|
||||
Jackson2ObjectMapperBuilder.class).build();
|
||||
Date date = new Date(1436966242231L);
|
||||
assertEquals("\"GMT+10:00\"", objectMapper.writeValueAsString(date));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class MockObjectMapperConfig {
|
||||
|
||||
|
||||
@@ -179,6 +179,7 @@ content into your application; rather pick only the properties that you need.
|
||||
spring.jackson.parser.*= # see Jackson's JsonParser.Feature
|
||||
spring.jackson.serialization.*= # see Jackson's SerializationFeature
|
||||
spring.jackson.serialization-inclusion= # Controls the inclusion of properties during serialization (see Jackson's JsonInclude.Include)
|
||||
spring.jackson.time-zone # Time zone used when formatting dates. Configured using any recognized time zone identifier, for example "America/Los_Angeles" or "GMT+10"
|
||||
|
||||
# THYMELEAF ({sc-spring-boot-autoconfigure}/thymeleaf/ThymeleafAutoConfiguration.{sc-ext}[ThymeleafAutoConfiguration])
|
||||
spring.thymeleaf.check-template-location=true
|
||||
|
||||
Reference in New Issue
Block a user