Merge pull request #3600 from joshiste/test-fix-locale
* pr/3600: Polish Add locale customization of the ObjectMapper
This commit is contained in:
@@ -20,6 +20,7 @@ import java.lang.reflect.Field;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Collection;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
@@ -64,6 +65,7 @@ import com.fasterxml.jackson.datatype.joda.ser.DateTimeSerializer;
|
||||
* @author Andy Wilkinson
|
||||
* @author Marcel Overdijk
|
||||
* @author Sebastien Deleuze
|
||||
* @author Johannes Stelzer
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Configuration
|
||||
@@ -162,6 +164,7 @@ public class JacksonAutoConfiguration {
|
||||
configureDateFormat(builder);
|
||||
configurePropertyNamingStrategy(builder);
|
||||
configureModules(builder);
|
||||
configureLocale(builder);
|
||||
return builder;
|
||||
}
|
||||
|
||||
@@ -237,6 +240,13 @@ public class JacksonAutoConfiguration {
|
||||
builder.modulesToInstall(moduleBeans.toArray(new Module[moduleBeans.size()]));
|
||||
}
|
||||
|
||||
private void configureLocale(Jackson2ObjectMapperBuilder builder) {
|
||||
Locale locale = this.jacksonProperties.getLocale();
|
||||
if (locale != null) {
|
||||
builder.locale(locale);
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> Collection<T> getBeans(ListableBeanFactory beanFactory,
|
||||
Class<T> type) {
|
||||
return BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory, type)
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.boot.autoconfigure.jackson;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
@@ -34,6 +35,7 @@ import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Marcel Overdijk
|
||||
* @author Johannes Stelzer
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.jackson")
|
||||
@@ -95,6 +97,11 @@ public class JacksonProperties {
|
||||
*/
|
||||
private TimeZone timeZone = null;
|
||||
|
||||
/**
|
||||
* Locale used for formatting.
|
||||
*/
|
||||
private Locale locale;
|
||||
|
||||
public String getDateFormat() {
|
||||
return this.dateFormat;
|
||||
}
|
||||
@@ -155,4 +162,12 @@ public class JacksonProperties {
|
||||
this.timeZone = timeZone;
|
||||
}
|
||||
|
||||
public Locale getLocale() {
|
||||
return this.locale;
|
||||
}
|
||||
|
||||
public void setLocale(Locale locale) {
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -72,6 +72,7 @@ import static org.mockito.Mockito.mock;
|
||||
* @author Andy Wilkinson
|
||||
* @author Marcel Overdijk
|
||||
* @author Sebastien Deleuze
|
||||
* @author Johannes Stelzer
|
||||
*/
|
||||
public class JacksonAutoConfigurationTests {
|
||||
|
||||
@@ -381,6 +382,7 @@ public class JacksonAutoConfigurationTests {
|
||||
"spring.jackson.time-zone:America/Los_Angeles");
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.jackson.date-format:zzzz");
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "spring.jackson.locale:en");
|
||||
this.context.refresh();
|
||||
ObjectMapper objectMapper = this.context.getBean(
|
||||
Jackson2ObjectMapperBuilder.class).build();
|
||||
@@ -402,6 +404,21 @@ public class JacksonAutoConfigurationTests {
|
||||
assertEquals("\"GMT+10:00\"", objectMapper.writeValueAsString(date));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customLocale() throws JsonProcessingException {
|
||||
this.context.register(JacksonAutoConfiguration.class);
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "spring.jackson.locale:de");
|
||||
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("\"Koordinierte Universalzeit\"",
|
||||
objectMapper.writeValueAsString(dateTime));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class MockObjectMapperConfig {
|
||||
|
||||
|
||||
@@ -176,6 +176,7 @@ content into your application; rather pick only the properties that you need.
|
||||
spring.jackson.deserialization.*= # see Jackson's DeserializationFeature
|
||||
spring.jackson.generator.*= # see Jackson's JsonGenerator.Feature
|
||||
spring.jackson.joda-date-time-format= # Joda date time format string
|
||||
spring.jackson.locale= # locale used for formatting
|
||||
spring.jackson.mapper.*= # see Jackson's MapperFeature
|
||||
spring.jackson.parser.*= # see Jackson's JsonParser.Feature
|
||||
spring.jackson.serialization.*= # see Jackson's SerializationFeature
|
||||
|
||||
Reference in New Issue
Block a user