Skip to content

Commit 8550d58

Browse files
committed
Merge pull request spring-projects#3600 from joshiste/test-fix-locale
* pr/3600: Polish Add locale customization of the ObjectMapper
2 parents 4c5a103 + 92871ea commit 8550d58

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.text.DateFormat;
2121
import java.text.SimpleDateFormat;
2222
import java.util.Collection;
23+
import java.util.Locale;
2324
import java.util.Map;
2425
import java.util.Map.Entry;
2526

@@ -64,6 +65,7 @@
6465
* @author Andy Wilkinson
6566
* @author Marcel Overdijk
6667
* @author Sebastien Deleuze
68+
* @author Johannes Stelzer
6769
* @since 1.1.0
6870
*/
6971
@Configuration
@@ -162,6 +164,7 @@ public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder() {
162164
configureDateFormat(builder);
163165
configurePropertyNamingStrategy(builder);
164166
configureModules(builder);
167+
configureLocale(builder);
165168
return builder;
166169
}
167170

@@ -237,6 +240,13 @@ private void configureModules(Jackson2ObjectMapperBuilder builder) {
237240
builder.modulesToInstall(moduleBeans.toArray(new Module[moduleBeans.size()]));
238241
}
239242

243+
private void configureLocale(Jackson2ObjectMapperBuilder builder) {
244+
Locale locale = this.jacksonProperties.getLocale();
245+
if (locale != null) {
246+
builder.locale(locale);
247+
}
248+
}
249+
240250
private static <T> Collection<T> getBeans(ListableBeanFactory beanFactory,
241251
Class<T> type) {
242252
return BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory, type)

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonProperties.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.autoconfigure.jackson;
1818

1919
import java.util.HashMap;
20+
import java.util.Locale;
2021
import java.util.Map;
2122
import java.util.TimeZone;
2223

@@ -34,6 +35,7 @@
3435
*
3536
* @author Andy Wilkinson
3637
* @author Marcel Overdijk
38+
* @author Johannes Stelzer
3739
* @since 1.2.0
3840
*/
3941
@ConfigurationProperties(prefix = "spring.jackson")
@@ -95,6 +97,11 @@ public class JacksonProperties {
9597
*/
9698
private TimeZone timeZone = null;
9799

100+
/**
101+
* Locale used for formatting.
102+
*/
103+
private Locale locale;
104+
98105
public String getDateFormat() {
99106
return this.dateFormat;
100107
}
@@ -155,4 +162,12 @@ public void setTimeZone(TimeZone timeZone) {
155162
this.timeZone = timeZone;
156163
}
157164

165+
public Locale getLocale() {
166+
return this.locale;
167+
}
168+
169+
public void setLocale(Locale locale) {
170+
this.locale = locale;
171+
}
172+
158173
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
* @author Andy Wilkinson
7373
* @author Marcel Overdijk
7474
* @author Sebastien Deleuze
75+
* @author Johannes Stelzer
7576
*/
7677
public class JacksonAutoConfigurationTests {
7778

@@ -381,6 +382,7 @@ public void customTimeZoneFormattingADateTime() throws JsonProcessingException {
381382
"spring.jackson.time-zone:America/Los_Angeles");
382383
EnvironmentTestUtils.addEnvironment(this.context,
383384
"spring.jackson.date-format:zzzz");
385+
EnvironmentTestUtils.addEnvironment(this.context, "spring.jackson.locale:en");
384386
this.context.refresh();
385387
ObjectMapper objectMapper = this.context.getBean(
386388
Jackson2ObjectMapperBuilder.class).build();
@@ -402,6 +404,21 @@ public void customTimeZoneFormattingADate() throws JsonProcessingException {
402404
assertEquals("\"GMT+10:00\"", objectMapper.writeValueAsString(date));
403405
}
404406

407+
@Test
408+
public void customLocale() throws JsonProcessingException {
409+
this.context.register(JacksonAutoConfiguration.class);
410+
EnvironmentTestUtils.addEnvironment(this.context, "spring.jackson.locale:de");
411+
EnvironmentTestUtils.addEnvironment(this.context,
412+
"spring.jackson.date-format:zzzz");
413+
this.context.refresh();
414+
ObjectMapper objectMapper = this.context
415+
.getBean(Jackson2ObjectMapperBuilder.class).build();
416+
417+
DateTime dateTime = new DateTime(1436966242231L, DateTimeZone.UTC);
418+
assertEquals("\"Koordinierte Universalzeit\"",
419+
objectMapper.writeValueAsString(dateTime));
420+
}
421+
405422
@Configuration
406423
protected static class MockObjectMapperConfig {
407424

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ content into your application; rather pick only the properties that you need.
176176
spring.jackson.deserialization.*= # see Jackson's DeserializationFeature
177177
spring.jackson.generator.*= # see Jackson's JsonGenerator.Feature
178178
spring.jackson.joda-date-time-format= # Joda date time format string
179+
spring.jackson.locale= # locale used for formatting
179180
spring.jackson.mapper.*= # see Jackson's MapperFeature
180181
spring.jackson.parser.*= # see Jackson's JsonParser.Feature
181182
spring.jackson.serialization.*= # see Jackson's SerializationFeature

0 commit comments

Comments
 (0)