diff --git a/spring-web/src/main/java/org/springframework/web/context/support/JacksonObjectMapperFactoryBean.java b/spring-web/src/main/java/org/springframework/http/converter/json/JacksonObjectMapperFactoryBean.java similarity index 83% rename from spring-web/src/main/java/org/springframework/web/context/support/JacksonObjectMapperFactoryBean.java rename to spring-web/src/main/java/org/springframework/http/converter/json/JacksonObjectMapperFactoryBean.java index cd0ef6b7253..765e174255b 100644 --- a/spring-web/src/main/java/org/springframework/web/context/support/JacksonObjectMapperFactoryBean.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/JacksonObjectMapperFactoryBean.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.context.support; +package org.springframework.http.converter.json; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -27,7 +27,7 @@ import org.codehaus.jackson.map.AnnotationIntrospector; import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.SerializationConfig; -import org.springframework.beans.FatalBeanException; + import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; @@ -79,12 +79,8 @@ import org.springframework.beans.factory.InitializingBean; * </bean> * * - *

Note: This BeanFctory is singleton, so if you need more than one, you'll - * need to configure multiple instances. - * * @author Dmitry Katsubo * @author Rossen Stoyanchev - * * @since 3.2 */ public class JacksonObjectMapperFactoryBean implements FactoryBean, InitializingBean { @@ -97,6 +93,7 @@ public class JacksonObjectMapperFactoryBean implements FactoryBean private DateFormat dateFormat; + /** * Set the ObjectMapper instance to use. * If not set an instance will be created using the default constructor. @@ -135,8 +132,8 @@ public class JacksonObjectMapperFactoryBean implements FactoryBean * {@link DeserializationConfig.Feature#AUTO_DETECT_FIELDS}. */ public void setAutoDetectFields(boolean autoDetectFields) { - this.features.put(DeserializationConfig.Feature.AUTO_DETECT_FIELDS, Boolean.valueOf(autoDetectFields)); - this.features.put(SerializationConfig.Feature.AUTO_DETECT_FIELDS, Boolean.valueOf(autoDetectFields)); + this.features.put(DeserializationConfig.Feature.AUTO_DETECT_FIELDS, autoDetectFields); + this.features.put(SerializationConfig.Feature.AUTO_DETECT_FIELDS, autoDetectFields); } /** @@ -144,22 +141,22 @@ public class JacksonObjectMapperFactoryBean implements FactoryBean * {@link DeserializationConfig.Feature#AUTO_DETECT_SETTERS}. */ public void setAutoDetectGettersSetters(boolean autoDetectGettersSetters) { - this.features.put(SerializationConfig.Feature.AUTO_DETECT_GETTERS, Boolean.valueOf(autoDetectGettersSetters)); - this.features.put(DeserializationConfig.Feature.AUTO_DETECT_SETTERS, Boolean.valueOf(autoDetectGettersSetters)); + this.features.put(SerializationConfig.Feature.AUTO_DETECT_GETTERS, autoDetectGettersSetters); + this.features.put(DeserializationConfig.Feature.AUTO_DETECT_SETTERS, autoDetectGettersSetters); } /** * Shortcut for {@link SerializationConfig.Feature#FAIL_ON_EMPTY_BEANS}. */ public void setFailOnEmptyBeans(boolean failOnEmptyBeans) { - this.features.put(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, Boolean.valueOf(failOnEmptyBeans)); + this.features.put(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, failOnEmptyBeans); } /** * Shortcut for {@link SerializationConfig.Feature#INDENT_OUTPUT}. */ public void setIndentOutput(boolean indentOutput) { - this.features.put(SerializationConfig.Feature.INDENT_OUTPUT, Boolean.valueOf(indentOutput)); + this.features.put(SerializationConfig.Feature.INDENT_OUTPUT, indentOutput); } /** @@ -170,11 +167,10 @@ public class JacksonObjectMapperFactoryBean implements FactoryBean * @see JsonGenerator.Feature */ public void setFeaturesToEnable(Object[] featuresToEnable) { - if (featuresToEnable == null) { - throw new FatalBeanException("featuresToEnable property should not be null"); - } - for (Object feature : featuresToEnable) { - this.features.put(feature, Boolean.TRUE); + if (featuresToEnable != null) { + for (Object feature : featuresToEnable) { + this.features.put(feature, Boolean.TRUE); + } } } @@ -186,47 +182,28 @@ public class JacksonObjectMapperFactoryBean implements FactoryBean * @see JsonGenerator.Feature */ public void setFeaturesToDisable(Object[] featuresToDisable) { - if (featuresToDisable == null) { - throw new FatalBeanException("featuresToDisable property should not be null"); + if (featuresToDisable != null) { + for (Object feature : featuresToDisable) { + this.features.put(feature, Boolean.FALSE); + } } - for (Object feature : featuresToDisable) { - this.features.put(feature, Boolean.FALSE); - } - } - - public ObjectMapper getObject() { - return this.objectMapper; } - public Class getObjectType() { - return ObjectMapper.class; - } - public boolean isSingleton() { - return true; - } - - /** - * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() - */ - public void afterPropertiesSet() throws FatalBeanException { + public void afterPropertiesSet() { if (this.objectMapper == null) { this.objectMapper = new ObjectMapper(); } - if (this.annotationIntrospector != null) { this.objectMapper.getSerializationConfig().setAnnotationIntrospector(annotationIntrospector); this.objectMapper.getDeserializationConfig().setAnnotationIntrospector(annotationIntrospector); } - if (this.dateFormat != null) { - // Deprecated for 1.8+, use - // objectMapper.setDateFormat(dateFormat); + // Deprecated for 1.8+, use objectMapper.setDateFormat(dateFormat); this.objectMapper.getSerializationConfig().setDateFormat(this.dateFormat); } - - for (Map.Entry entry : features.entrySet()) { - setFeatureEnabled(entry.getKey(), entry.getValue().booleanValue()); + for (Map.Entry entry : this.features.entrySet()) { + setFeatureEnabled(entry.getKey(), entry.getValue()); } } @@ -244,7 +221,21 @@ public class JacksonObjectMapperFactoryBean implements FactoryBean this.objectMapper.configure((JsonGenerator.Feature) feature, enabled); } else { - throw new FatalBeanException("Unknown feature class " + feature.getClass().getName()); + throw new IllegalArgumentException("Unknown feature class: " + feature.getClass().getName()); } } + + + public ObjectMapper getObject() { + return this.objectMapper; + } + + public Class getObjectType() { + return ObjectMapper.class; + } + + public boolean isSingleton() { + return true; + } + } diff --git a/spring-web/src/test/java/org/springframework/web/context/support/JacksonObjectMapperFactoryBeanTests.java b/spring-web/src/test/java/org/springframework/http/converter/json/JacksonObjectMapperFactoryBeanTests.java similarity index 92% rename from spring-web/src/test/java/org/springframework/web/context/support/JacksonObjectMapperFactoryBeanTests.java rename to spring-web/src/test/java/org/springframework/http/converter/json/JacksonObjectMapperFactoryBeanTests.java index 3a8923ad1be..3a22dfddf1a 100644 --- a/spring-web/src/test/java/org/springframework/web/context/support/JacksonObjectMapperFactoryBeanTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/json/JacksonObjectMapperFactoryBeanTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.web.context.support; +package org.springframework.http.converter.json; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -33,10 +33,9 @@ import org.codehaus.jackson.map.introspect.NopAnnotationIntrospector; import org.junit.Before; import org.junit.Test; import org.springframework.beans.FatalBeanException; +import org.springframework.http.converter.json.JacksonObjectMapperFactoryBean; /** - * Test cases for {@link JacksonObjectMapperFactoryBean} class. - * * @author Dmitry Katsubo */ public class JacksonObjectMapperFactoryBeanTests { @@ -50,24 +49,13 @@ public class JacksonObjectMapperFactoryBeanTests { factory = new JacksonObjectMapperFactoryBean(); } - @Test(expected=FatalBeanException.class) - public void testSetFeaturesToEnableNull() throws Exception { - factory.setFeaturesToEnable(null); - factory.setFeaturesToEnable(new Object[0]); - } - - @Test(expected=FatalBeanException.class) - public void testSetFeaturesToDisableNull() { - factory.setFeaturesToDisable(null); - } - @Test public void testSetFeaturesToEnableEmpty() { factory.setFeaturesToEnable(new Object[0]); factory.setFeaturesToDisable(new Object[0]); } - @Test(expected = FatalBeanException.class) + @Test(expected = IllegalArgumentException.class) public void testUnknownFeature() { factory.setFeaturesToEnable(new Object[] { Boolean.TRUE }); factory.afterPropertiesSet(); @@ -168,4 +156,5 @@ public class JacksonObjectMapperFactoryBeanTests { assertFalse(jsonFactory.isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE)); assertFalse(jsonFactory.isEnabled(JsonGenerator.Feature.QUOTE_FIELD_NAMES)); } + }