Skip to content

Commit 64461bb

Browse files
Johannes Stelzersnicoll
authored andcommitted
Add locale customization of the ObjectMapper
Add a new spring.jackson.locale property to customize the locale of the ObjectMapper. Closes spring-projectsgh-3600
1 parent 4c5a103 commit 64461bb

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder() {
162162
configureDateFormat(builder);
163163
configurePropertyNamingStrategy(builder);
164164
configureModules(builder);
165+
configureLocale(builder);
165166
return builder;
166167
}
167168

@@ -237,6 +238,13 @@ private void configureModules(Jackson2ObjectMapperBuilder builder) {
237238
builder.modulesToInstall(moduleBeans.toArray(new Module[moduleBeans.size()]));
238239
}
239240

241+
private void configureLocale(Jackson2ObjectMapperBuilder builder) {
242+
String locale = this.jacksonProperties.getLocale();
243+
if (locale != null) {
244+
builder.locale(locale);
245+
}
246+
}
247+
240248
private static <T> Collection<T> getBeans(ListableBeanFactory beanFactory,
241249
Class<T> type) {
242250
return BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory, type)

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public class JacksonProperties {
9595
*/
9696
private TimeZone timeZone = null;
9797

98+
/**
99+
* Locale used for formatting.
100+
*/
101+
private String locale;
102+
98103
public String getDateFormat() {
99104
return this.dateFormat;
100105
}
@@ -155,4 +160,12 @@ public void setTimeZone(TimeZone timeZone) {
155160
this.timeZone = timeZone;
156161
}
157162

163+
public String getLocale() {
164+
return this.locale;
165+
}
166+
167+
public void setLocale(String locale) {
168+
this.locale = locale;
169+
}
170+
158171
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ public void customTimeZoneFormattingADateTime() throws JsonProcessingException {
381381
"spring.jackson.time-zone:America/Los_Angeles");
382382
EnvironmentTestUtils.addEnvironment(this.context,
383383
"spring.jackson.date-format:zzzz");
384+
EnvironmentTestUtils.addEnvironment(this.context, "spring.jackson.locale:en");
384385
this.context.refresh();
385386
ObjectMapper objectMapper = this.context.getBean(
386387
Jackson2ObjectMapperBuilder.class).build();
@@ -402,6 +403,21 @@ public void customTimeZoneFormattingADate() throws JsonProcessingException {
402403
assertEquals("\"GMT+10:00\"", objectMapper.writeValueAsString(date));
403404
}
404405

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

0 commit comments

Comments
 (0)