diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java b/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java index 5af47d9ee2c..d0871c86286 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java @@ -49,6 +49,7 @@ import org.springframework.beans.FatalBeanException; import org.springframework.context.ApplicationContext; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.StringUtils; /** * A builder used to create {@link ObjectMapper} instances with a fluent API. @@ -74,7 +75,7 @@ import org.springframework.util.ClassUtils; */ public class Jackson2ObjectMapperBuilder { - private boolean createXmlMapper; + private boolean createXmlMapper = false; private DateFormat dateFormat; @@ -100,7 +101,7 @@ public class Jackson2ObjectMapperBuilder { private Class[] moduleClasses; - private boolean findModulesViaServiceLoader; + private boolean findModulesViaServiceLoader = false; private boolean findWellKnownModules = true; @@ -153,6 +154,17 @@ public class Jackson2ObjectMapperBuilder { return this; } + /** + * Override the default {@link Locale} to use for formatting. + * Default value used is {@link Locale#getDefault()}. + * @param localeString the locale ID as a String representation + * @since 4.1.5 + */ + public Jackson2ObjectMapperBuilder locale(String localeString) { + this.locale = StringUtils.parseLocaleString(localeString); + return this; + } + /** * Override the default {@link TimeZone} to use for formatting. * Default value used is UTC (NOT local timezone). @@ -166,11 +178,11 @@ public class Jackson2ObjectMapperBuilder { /** * Override the default {@link TimeZone} to use for formatting. * Default value used is UTC (NOT local timezone). - * @param zoneId the time-zone ID + * @param timeZoneString the zone ID as a String representation * @since 4.1.5 */ - public Jackson2ObjectMapperBuilder timeZone(String zoneId) { - this.timeZone = TimeZone.getTimeZone(zoneId); + public Jackson2ObjectMapperBuilder timeZone(String timeZoneString) { + this.timeZone = StringUtils.parseTimeZoneString(timeZoneString); return this; } @@ -528,8 +540,8 @@ public class Jackson2ObjectMapperBuilder { else if (this.findWellKnownModules) { registerWellKnownModulesIfAvailable(objectMapper); } + if (this.modules != null) { - // Complete list of modules given for (Module module : this.modules) { // Using Jackson 2.0+ registerModule method, not Jackson 2.2+ registerModules objectMapper.registerModule(module); diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java b/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java index 4288b03b975..0058b0f74ce 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java @@ -191,16 +191,6 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean { @Override - public void serialize(Integer value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { + public void serialize(Integer value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); gen.writeNumberField("customid", value); gen.writeEndObject(); diff --git a/spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBeanTests.java b/spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBeanTests.java index a85370ff655..c4ccaea1c1f 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBeanTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBeanTests.java @@ -55,7 +55,6 @@ import com.fasterxml.jackson.databind.ser.std.ClassSerializer; import com.fasterxml.jackson.databind.ser.std.NumberSerializer; import com.fasterxml.jackson.databind.type.SimpleType; import com.fasterxml.jackson.dataformat.xml.XmlMapper; -import static org.hamcrest.Matchers.containsString; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.junit.Before; @@ -63,6 +62,7 @@ import org.junit.Test; import org.springframework.beans.FatalBeanException; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; /** @@ -198,7 +198,7 @@ public class Jackson2ObjectMapperFactoryBeanTests { public void timeZoneStringSetter() { String zoneId = "Europe/Paris"; - this.factory.setTimeZone(zoneId); + this.factory.setTimeZone(TimeZone.getTimeZone(zoneId)); this.factory.afterPropertiesSet(); TimeZone timeZone = TimeZone.getTimeZone(zoneId); @@ -210,7 +210,7 @@ public class Jackson2ObjectMapperFactoryBeanTests { public void wrongTimeZoneStringSetter() { String zoneId = "foo"; - this.factory.setTimeZone(zoneId); + this.factory.setTimeZone(TimeZone.getTimeZone(zoneId)); this.factory.afterPropertiesSet(); TimeZone timeZone = TimeZone.getTimeZone("GMT"); @@ -224,7 +224,7 @@ public class Jackson2ObjectMapperFactoryBeanTests { SimpleModule module = new SimpleModule(); module.addSerializer(Integer.class, serializer1); - this.factory.setModules(Arrays.asList(new Module[] {module})); + this.factory.setModules(Arrays.asList(new Module[]{module})); this.factory.afterPropertiesSet(); ObjectMapper objectMapper = this.factory.getObject(); @@ -397,6 +397,7 @@ public class Jackson2ObjectMapperFactoryBeanTests { assertEquals(XmlMapper.class, this.factory.getObjectType()); } + public static class CustomIntegerModule extends Module { @Override @@ -417,10 +418,11 @@ public class Jackson2ObjectMapperFactoryBeanTests { } } + public static class CustomIntegerSerializer extends JsonSerializer { @Override - public void serialize(Integer value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { + public void serialize(Integer value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); gen.writeNumberField("customid", value); gen.writeEndObject();