Skip to content

Commit 77fcf21

Browse files
committed
Refined Jackson configuration enhancements
Issue: SPR-12594 Issue: SPR-12634
1 parent 7a6a132 commit 77fcf21

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.springframework.context.ApplicationContext;
5050
import org.springframework.util.Assert;
5151
import org.springframework.util.ClassUtils;
52+
import org.springframework.util.StringUtils;
5253

5354
/**
5455
* A builder used to create {@link ObjectMapper} instances with a fluent API.
@@ -74,7 +75,7 @@
7475
*/
7576
public class Jackson2ObjectMapperBuilder {
7677

77-
private boolean createXmlMapper;
78+
private boolean createXmlMapper = false;
7879

7980
private DateFormat dateFormat;
8081

@@ -100,7 +101,7 @@ public class Jackson2ObjectMapperBuilder {
100101

101102
private Class<? extends Module>[] moduleClasses;
102103

103-
private boolean findModulesViaServiceLoader;
104+
private boolean findModulesViaServiceLoader = false;
104105

105106
private boolean findWellKnownModules = true;
106107

@@ -153,6 +154,17 @@ public Jackson2ObjectMapperBuilder locale(Locale locale) {
153154
return this;
154155
}
155156

157+
/**
158+
* Override the default {@link Locale} to use for formatting.
159+
* Default value used is {@link Locale#getDefault()}.
160+
* @param localeString the locale ID as a String representation
161+
* @since 4.1.5
162+
*/
163+
public Jackson2ObjectMapperBuilder locale(String localeString) {
164+
this.locale = StringUtils.parseLocaleString(localeString);
165+
return this;
166+
}
167+
156168
/**
157169
* Override the default {@link TimeZone} to use for formatting.
158170
* Default value used is UTC (NOT local timezone).
@@ -166,11 +178,11 @@ public Jackson2ObjectMapperBuilder timeZone(TimeZone timeZone) {
166178
/**
167179
* Override the default {@link TimeZone} to use for formatting.
168180
* Default value used is UTC (NOT local timezone).
169-
* @param zoneId the time-zone ID
181+
* @param timeZoneString the zone ID as a String representation
170182
* @since 4.1.5
171183
*/
172-
public Jackson2ObjectMapperBuilder timeZone(String zoneId) {
173-
this.timeZone = TimeZone.getTimeZone(zoneId);
184+
public Jackson2ObjectMapperBuilder timeZone(String timeZoneString) {
185+
this.timeZone = StringUtils.parseTimeZoneString(timeZoneString);
174186
return this;
175187
}
176188

@@ -528,8 +540,8 @@ public void configure(ObjectMapper objectMapper) {
528540
else if (this.findWellKnownModules) {
529541
registerWellKnownModulesIfAvailable(objectMapper);
530542
}
543+
531544
if (this.modules != null) {
532-
// Complete list of modules given
533545
for (Module module : this.modules) {
534546
// Using Jackson 2.0+ registerModule method, not Jackson 2.2+ registerModules
535547
objectMapper.registerModule(module);

spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,6 @@ public void setTimeZone(TimeZone timeZone) {
191191
this.builder.timeZone(timeZone);
192192
}
193193

194-
/**
195-
* Override the default {@link TimeZone} to use for formatting.
196-
* Default value used is UTC (NOT local timezone).
197-
* @param zoneId the time-zone ID
198-
* @since 4.1.5
199-
*/
200-
public void setTimeZone(String zoneId) {
201-
this.builder.timeZone(zoneId);
202-
}
203-
204194
/**
205195
* Set an {@link AnnotationIntrospector} for both serialization and deserialization.
206196
*/

spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilderTests.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@
5656
import com.fasterxml.jackson.databind.ser.std.NumberSerializer;
5757
import com.fasterxml.jackson.databind.type.SimpleType;
5858
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
59-
import static org.hamcrest.Matchers.containsString;
6059
import org.joda.time.DateTime;
6160
import org.joda.time.DateTimeZone;
6261
import org.junit.Test;
6362

6463
import org.springframework.beans.FatalBeanException;
6564

65+
import static org.hamcrest.Matchers.*;
6666
import static org.junit.Assert.*;
6767

6868
/**
@@ -201,13 +201,10 @@ public void timeZoneStringSetter() {
201201
assertEquals(timeZone, objectMapper.getDeserializationConfig().getTimeZone());
202202
}
203203

204-
@Test
204+
@Test(expected = IllegalArgumentException.class)
205205
public void wrongTimeZoneStringSetter() {
206206
String zoneId = "foo";
207-
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().timeZone(zoneId).build();
208-
TimeZone timeZone = TimeZone.getTimeZone("GMT");
209-
assertEquals(timeZone, objectMapper.getSerializationConfig().getTimeZone());
210-
assertEquals(timeZone, objectMapper.getDeserializationConfig().getTimeZone());
207+
Jackson2ObjectMapperBuilder.json().timeZone(zoneId).build();
211208
}
212209

213210
@Test
@@ -428,10 +425,11 @@ public void setupModule(SetupContext context) {
428425
}
429426
}
430427

428+
431429
public static class CustomIntegerSerializer extends JsonSerializer<Integer> {
432430

433431
@Override
434-
public void serialize(Integer value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
432+
public void serialize(Integer value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
435433
gen.writeStartObject();
436434
gen.writeNumberField("customid", value);
437435
gen.writeEndObject();

spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBeanTests.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@
5555
import com.fasterxml.jackson.databind.ser.std.NumberSerializer;
5656
import com.fasterxml.jackson.databind.type.SimpleType;
5757
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
58-
import static org.hamcrest.Matchers.containsString;
5958
import org.joda.time.DateTime;
6059
import org.joda.time.DateTimeZone;
6160
import org.junit.Before;
6261
import org.junit.Test;
6362

6463
import org.springframework.beans.FatalBeanException;
6564

65+
import static org.hamcrest.Matchers.*;
6666
import static org.junit.Assert.*;
6767

6868
/**
@@ -198,7 +198,7 @@ public void timeZoneSetter() {
198198
public void timeZoneStringSetter() {
199199
String zoneId = "Europe/Paris";
200200

201-
this.factory.setTimeZone(zoneId);
201+
this.factory.setTimeZone(TimeZone.getTimeZone(zoneId));
202202
this.factory.afterPropertiesSet();
203203

204204
TimeZone timeZone = TimeZone.getTimeZone(zoneId);
@@ -210,7 +210,7 @@ public void timeZoneStringSetter() {
210210
public void wrongTimeZoneStringSetter() {
211211
String zoneId = "foo";
212212

213-
this.factory.setTimeZone(zoneId);
213+
this.factory.setTimeZone(TimeZone.getTimeZone(zoneId));
214214
this.factory.afterPropertiesSet();
215215

216216
TimeZone timeZone = TimeZone.getTimeZone("GMT");
@@ -224,7 +224,7 @@ public void setModules() {
224224
SimpleModule module = new SimpleModule();
225225
module.addSerializer(Integer.class, serializer1);
226226

227-
this.factory.setModules(Arrays.asList(new Module[] {module}));
227+
this.factory.setModules(Arrays.asList(new Module[]{module}));
228228
this.factory.afterPropertiesSet();
229229
ObjectMapper objectMapper = this.factory.getObject();
230230

@@ -397,6 +397,7 @@ public void createXmlMapper() {
397397
assertEquals(XmlMapper.class, this.factory.getObjectType());
398398
}
399399

400+
400401
public static class CustomIntegerModule extends Module {
401402

402403
@Override
@@ -417,10 +418,11 @@ public void setupModule(SetupContext context) {
417418
}
418419
}
419420

421+
420422
public static class CustomIntegerSerializer extends JsonSerializer<Integer> {
421423

422424
@Override
423-
public void serialize(Integer value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
425+
public void serialize(Integer value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
424426
gen.writeStartObject();
425427
gen.writeNumberField("customid", value);
426428
gen.writeEndObject();

0 commit comments

Comments
 (0)