Skip to content

Commit 268657b

Browse files
committed
Add PropertyNamingStrategy field to ObjectMapperFB
Issue: SPR-11431
1 parent 1dedb67 commit 268657b

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
3434
import com.fasterxml.jackson.databind.MapperFeature;
3535
import com.fasterxml.jackson.databind.Module;
3636
import com.fasterxml.jackson.databind.ObjectMapper;
37+
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
3738
import com.fasterxml.jackson.databind.SerializationFeature;
3839
import com.fasterxml.jackson.databind.module.SimpleModule;
3940

@@ -143,6 +144,8 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
143144

144145
private boolean findModulesViaServiceLoader;
145146

147+
private PropertyNamingStrategy propertyNamingStrategy;
148+
146149
private ClassLoader beanClassLoader;
147150

148151

@@ -328,6 +331,15 @@ public void setFindModulesViaServiceLoader(boolean findModules) {
328331
this.findModulesViaServiceLoader = findModules;
329332
}
330333

334+
/**
335+
* Specify a {@link com.fasterxml.jackson.databind.PropertyNamingStrategy} to
336+
* configure the {@link ObjectMapper} with.
337+
* @@since 4.0.2
338+
*/
339+
public void setPropertyNamingStrategy(PropertyNamingStrategy propertyNamingStrategy) {
340+
this.propertyNamingStrategy = propertyNamingStrategy;
341+
}
342+
331343
@Override
332344
public void setBeanClassLoader(ClassLoader beanClassLoader) {
333345
this.beanClassLoader = beanClassLoader;
@@ -385,6 +397,10 @@ public void afterPropertiesSet() {
385397
registerWellKnownModulesIfAvailable();
386398
}
387399
}
400+
401+
if (this.propertyNamingStrategy != null) {
402+
this.objectMapper.setPropertyNamingStrategy(this.propertyNamingStrategy);
403+
}
388404
}
389405

390406
@SuppressWarnings("unchecked")

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
import com.fasterxml.jackson.databind.MapperFeature;
3333
import com.fasterxml.jackson.databind.Module;
3434
import com.fasterxml.jackson.databind.ObjectMapper;
35+
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
3536
import com.fasterxml.jackson.databind.SerializationFeature;
3637
import com.fasterxml.jackson.databind.cfg.DeserializerFactoryConfig;
3738
import com.fasterxml.jackson.databind.cfg.SerializerFactoryConfig;
@@ -49,7 +50,12 @@
4950

5051
import org.springframework.beans.FatalBeanException;
5152

52-
import static org.junit.Assert.*;
53+
import static junit.framework.Assert.assertTrue;
54+
import static org.junit.Assert.assertEquals;
55+
import static org.junit.Assert.assertFalse;
56+
import static org.junit.Assert.assertNotNull;
57+
import static org.junit.Assert.assertNull;
58+
import static org.junit.Assert.assertSame;
5359

5460
/**
5561
* Test cases for {@link Jackson2ObjectMapperFactoryBean} class.
@@ -186,6 +192,16 @@ private static DeserializerFactoryConfig getDeserializerFactoryConfig(ObjectMapp
186192
return ((BasicDeserializerFactory) objectMapper.getDeserializationContext().getFactory()).getFactoryConfig();
187193
}
188194

195+
@Test
196+
public void testPropertyNamingStrategy() {
197+
PropertyNamingStrategy strategy = new PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy();
198+
this.factory.setPropertyNamingStrategy(strategy);
199+
this.factory.afterPropertiesSet();
200+
201+
assertSame(strategy, this.factory.getObject().getSerializationConfig().getPropertyNamingStrategy());
202+
assertSame(strategy, this.factory.getObject().getDeserializationConfig().getPropertyNamingStrategy());
203+
}
204+
189205
@Test
190206
public void testCompleteSetup() {
191207
NopAnnotationIntrospector annotationIntrospector = NopAnnotationIntrospector.instance;

0 commit comments

Comments
 (0)