Upgraded to Jackson 1.9 (raising minimum to 1.8+) and 2.2

This commit is contained in:
Juergen Hoeller
2013-05-02 15:51:59 +02:00
parent 9f9b972f00
commit 767bd3f3f8
11 changed files with 164 additions and 188 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,11 +22,6 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
@@ -38,9 +33,14 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
/**
* A FactoryBean for creating a Jackson {@link ObjectMapper} with setters to
* enable or disable Jackson features from within XML configuration.
* A {@link FactoryBean} for creating a Jackson 2.x {@link ObjectMapper} with setters
* to enable or disable Jackson features from within XML configuration.
*
* <p>Example usage with
* {@link org.springframework.http.converter.json.MappingJackson2HttpMessageConverter}:
@@ -56,7 +56,7 @@ import com.fasterxml.jackson.databind.module.SimpleModule;
* &lt;/bean>
* </pre>
*
* <p>Example usage with {@link org.springframework.web.servlet.view.json.MappingJackson2JsonView}:
* <p>Example usage with MappingJackson2JsonView:
*
* <pre>
* &lt;bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
@@ -94,12 +94,8 @@ import com.fasterxml.jackson.databind.module.SimpleModule;
* &lt;/bean>
* </pre>
*
* <p>Note: This BeanFctory is singleton, so if you need more than one you'll need
* to configure multiple instances.
*
* @author <a href="mailto:dmitry.katsubo@gmail.com">Dmitry Katsubo</a>
* @author Rossen Stoyanchev
*
* @since 3.2
*/
public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper>, InitializingBean {
@@ -197,8 +193,8 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
* {@link MapperFeature#AUTO_DETECT_GETTERS} option.
*/
public void setAutoDetectGettersSetters(boolean autoDetectGettersSetters) {
this.features.put(MapperFeature.AUTO_DETECT_SETTERS, autoDetectGettersSetters);
this.features.put(MapperFeature.AUTO_DETECT_GETTERS, autoDetectGettersSetters);
this.features.put(MapperFeature.AUTO_DETECT_SETTERS, autoDetectGettersSetters);
}
/**
@@ -217,12 +213,9 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
/**
* Specify features to enable.
*
* @see MapperFeature
* @see SerializationFeature
* @see DeserializationFeature
* @see org.codehaus.jackson.map.JsonParser.Feature
* @see org.codehaus.jackson.map.JsonGenerator.Feature
*/
public void setFeaturesToEnable(Object... featuresToEnable) {
if (featuresToEnable != null) {
@@ -234,12 +227,11 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
/**
* Specify features to disable.
*
* @see MapperFeature
* @see SerializationFeature
* @see DeserializationFeature
* @see org.codehaus.jackson.map.JsonParser.Feature
* @see org.codehaus.jackson.map.JsonGenerator.Feature
* @see com.fasterxml.jackson.core.JsonParser.Feature
* @see com.fasterxml.jackson.core.JsonGenerator.Feature
* @see com.fasterxml.jackson.databind.SerializationFeature
* @see com.fasterxml.jackson.databind.DeserializationFeature
* @see com.fasterxml.jackson.databind.MapperFeature
*/
public void setFeaturesToDisable(Object... featuresToDisable) {
if (featuresToDisable != null) {
@@ -249,7 +241,8 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
}
}
public void afterPropertiesSet() throws FatalBeanException {
public void afterPropertiesSet() {
if (this.objectMapper == null) {
this.objectMapper = new ObjectMapper();
}
@@ -258,7 +251,7 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
this.objectMapper.setDateFormat(this.dateFormat);
}
if (this.serializers != null || this.deserializers != null) {
if (!this.serializers.isEmpty() || !this.deserializers.isEmpty()) {
SimpleModule module = new SimpleModule();
addSerializers(module);
addDeserializers(module);
@@ -289,26 +282,27 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
}
private void configureFeature(Object feature, boolean enabled) {
if (feature instanceof MapperFeature) {
this.objectMapper.configure((MapperFeature) feature, enabled);
}
else if (feature instanceof DeserializationFeature) {
this.objectMapper.configure((DeserializationFeature) feature, enabled);
}
else if (feature instanceof SerializationFeature) {
this.objectMapper.configure((SerializationFeature) feature, enabled);
}
else if (feature instanceof JsonParser.Feature) {
if (feature instanceof JsonParser.Feature) {
this.objectMapper.configure((JsonParser.Feature) feature, enabled);
}
else if (feature instanceof JsonGenerator.Feature) {
this.objectMapper.configure((JsonGenerator.Feature) feature, enabled);
}
else if (feature instanceof SerializationFeature) {
this.objectMapper.configure((SerializationFeature) feature, enabled);
}
else if (feature instanceof DeserializationFeature) {
this.objectMapper.configure((DeserializationFeature) feature, enabled);
}
else if (feature instanceof MapperFeature) {
this.objectMapper.configure((MapperFeature) feature, enabled);
}
else {
throw new FatalBeanException("Unknown feature class " + feature.getClass().getName());
}
}
/**
* Return the singleton ObjectMapper.
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,8 +32,8 @@ import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
/**
* A FactoryBean for creating a Jackson {@link ObjectMapper} with setters to
* enable or disable Jackson features from within XML configuration.
* A {@link FactoryBean} for creating a Jackson 1.x {@link ObjectMapper} with setters
* to enable or disable Jackson features from within XML configuration.
*
* <p>Example usage with MappingJacksonHttpMessageConverter:
* <pre>
@@ -79,6 +79,8 @@ import org.springframework.beans.factory.InitializingBean;
* &lt;/bean>
* </pre>
*
* <p><b>NOTE:</b> Requires Jackson 1.8 or higher, as of Spring 4.0.
*
* @author <a href="mailto:dmitry.katsubo@gmail.com">Dmitry Katsubo</a>
* @author Rossen Stoyanchev
* @since 3.2
@@ -161,11 +163,10 @@ public class JacksonObjectMapperFactoryBean implements FactoryBean<ObjectMapper>
/**
* Specify features to enable.
*
* @see org.codehaus.jackson.JsonParser.Feature
* @see org.codehaus.jackson.JsonGenerator.Feature
* @see org.codehaus.jackson.map.SerializationConfig.Feature
* @see org.codehaus.jackson.map.DeserializationConfig.Feature
* @see org.codehaus.jackson.map.JsonParser.Feature
* @see org.codehaus.jackson.map.JsonGenerator.Feature
*/
public void setFeaturesToEnable(Object[] featuresToEnable) {
if (featuresToEnable != null) {
@@ -177,11 +178,10 @@ public class JacksonObjectMapperFactoryBean implements FactoryBean<ObjectMapper>
/**
* Specify features to disable.
*
* @see org.codehaus.jackson.JsonParser.Feature
* @see org.codehaus.jackson.JsonGenerator.Feature
* @see org.codehaus.jackson.map.SerializationConfig.Feature
* @see org.codehaus.jackson.map.DeserializationConfig.Feature
* @see org.codehaus.jackson.map.JsonParser.Feature
* @see org.codehaus.jackson.map.JsonGenerator.Feature
*/
public void setFeaturesToDisable(Object[] featuresToDisable) {
if (featuresToDisable != null) {
@@ -191,41 +191,44 @@ public class JacksonObjectMapperFactoryBean implements FactoryBean<ObjectMapper>
}
}
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);
this.objectMapper.setSerializationConfig(
this.objectMapper.getSerializationConfig().withAnnotationIntrospector(this.annotationIntrospector));
this.objectMapper.setDeserializationConfig(
this.objectMapper.getDeserializationConfig().withAnnotationIntrospector(this.annotationIntrospector));
}
if (this.dateFormat != null) {
// Deprecated for 1.8+, use objectMapper.setDateFormat(dateFormat);
this.objectMapper.getSerializationConfig().setDateFormat(this.dateFormat);
this.objectMapper.setDateFormat(this.dateFormat);
}
for (Map.Entry<Object, Boolean> entry : this.features.entrySet()) {
configureFeature(entry.getKey(), entry.getValue().booleanValue());
configureFeature(entry.getKey(), entry.getValue());
}
}
private void configureFeature(Object feature, boolean enabled) {
if (feature instanceof DeserializationConfig.Feature) {
this.objectMapper.configure((DeserializationConfig.Feature) feature, enabled);
}
else if (feature instanceof SerializationConfig.Feature) {
this.objectMapper.configure((SerializationConfig.Feature) feature, enabled);
}
else if (feature instanceof JsonParser.Feature) {
if (feature instanceof JsonParser.Feature) {
this.objectMapper.configure((JsonParser.Feature) feature, enabled);
}
else if (feature instanceof JsonGenerator.Feature) {
this.objectMapper.configure((JsonGenerator.Feature) feature, enabled);
}
else if (feature instanceof SerializationConfig.Feature) {
this.objectMapper.configure((SerializationConfig.Feature) feature, enabled);
}
else if (feature instanceof DeserializationConfig.Feature) {
this.objectMapper.configure((DeserializationConfig.Feature) feature, enabled);
}
else {
throw new IllegalArgumentException("Unknown feature class: " + feature.getClass().getName());
}
}
/**
* Return the singleton ObjectMapper.
*/

View File

@@ -39,8 +39,8 @@ import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.Assert;
/**
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter}
* that can read and write JSON using <a href="http://jackson.codehaus.org/">Jackson 2's</a> {@link ObjectMapper}.
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter} that
* can read and write JSON using <a href="http://jackson.codehaus.org/">Jackson 2.x's</a> {@link ObjectMapper}.
*
* <p>This converter can be used to bind to typed beans, or untyped {@link java.util.HashMap HashMap} instances.
*
@@ -178,6 +178,8 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv
throws IOException, HttpMessageNotWritableException {
JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
// The following has been deprecated as late as Jackson 2.2 (April 2013);
// preserved for the time being, for Jackson 2.0/2.1 compatibility.
JsonGenerator jsonGenerator =
this.objectMapper.getJsonFactory().createJsonGenerator(outputMessage.getBody(), encoding);
@@ -200,8 +202,7 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv
/**
* Return the Jackson {@link JavaType} for the specified type and context class.
* <p>The default implementation returns {@link ObjectMapper#constructType(java.lang.reflect.Type)}
* or {@code ObjectMapper.getTypeFactory().constructType(type, contextClass)},
* <p>The default implementation returns {@code typeFactory.constructType(type, contextClass)},
* but this can be overridden in subclasses, to allow for custom generic collection handling.
* For instance:
* <pre class="code">
@@ -220,9 +221,7 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv
* @return the java type
*/
protected JavaType getJavaType(Type type, Class<?> contextClass) {
return (contextClass != null) ?
this.objectMapper.getTypeFactory().constructType(type, contextClass) :
this.objectMapper.constructType(type);
return this.objectMapper.getTypeFactory().constructType(type, contextClass);
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,7 +26,6 @@ import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.type.TypeFactory;
import org.codehaus.jackson.type.JavaType;
import org.springframework.http.HttpInputMessage;
@@ -39,8 +38,8 @@ import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.Assert;
/**
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter}
* that can read and write JSON using <a href="http://jackson.codehaus.org/">Jackson's</a> {@link ObjectMapper}.
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter} that
* can read and write JSON using <a href="http://jackson.codehaus.org/">Jackson 1.x's</a> {@link ObjectMapper}.
*
* <p>This converter can be used to bind to typed beans, or untyped {@link java.util.HashMap HashMap} instances.
*
@@ -111,7 +110,7 @@ public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConve
}
/**
* Whether to use the {@link org.codehaus.jackson.impl.DefaultPrettyPrinter} when writing JSON.
* Whether to use the {@link org.codehaus.jackson.util.DefaultPrettyPrinter} when writing JSON.
* This is a shortcut for setting up an {@code ObjectMapper} as follows:
* <pre>
* ObjectMapper mapper = new ObjectMapper();
@@ -125,6 +124,7 @@ public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConve
configurePrettyPrint();
}
@Override
public boolean canRead(Class<?> clazz, MediaType mediaType) {
return canRead(clazz, null, mediaType);
@@ -197,8 +197,7 @@ public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConve
/**
* Return the Jackson {@link JavaType} for the specified type and context class.
* <p>The default implementation returns {@link TypeFactory#type(java.lang.reflect.Type)}
* or {@code TypeFactory.type(type, TypeFactory.type(contextClass))},
* <p>The default implementation returns {@code typeFactory.constructType(type, contextClass)},
* but this can be overridden in subclasses, to allow for custom generic collection handling.
* For instance:
* <pre class="code">
@@ -216,9 +215,7 @@ public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConve
* @return the java type
*/
protected JavaType getJavaType(Type type, Class<?> contextClass) {
return (contextClass != null) ?
TypeFactory.type(type, TypeFactory.type(contextClass)) :
TypeFactory.type(type);
return this.objectMapper.getTypeFactory().constructType(type, contextClass);
}
/**