MappingJackson2(Http)MessageConverter logs warnings after canRead/canWrite checks

This change involves a general upgrade to Jackson 2.3 in our build.

Issue: SPR-11261
This commit is contained in:
Juergen Hoeller
2013-12-29 21:49:53 +01:00
parent 7f5d6ea3f9
commit 85921808b3
4 changed files with 96 additions and 39 deletions

View File

@@ -19,6 +19,15 @@ package org.springframework.http.converter.json;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.concurrent.atomic.AtomicReference;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
@@ -28,14 +37,7 @@ import org.springframework.http.converter.GenericHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.Assert;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.util.ClassUtils;
/**
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter} that
@@ -46,11 +48,12 @@ import com.fasterxml.jackson.databind.SerializationFeature;
* <p>By default, this converter supports {@code application/json}. This can be overridden by setting the
* {@link #setSupportedMediaTypes supportedMediaTypes} property.
*
* <p>Tested against Jackson 2.2; compatible with Jackson 2.0 and higher.
* <p>Tested against Jackson 2.2 and 2.3; compatible with Jackson 2.0 and higher.
*
* @author Arjen Poutsma
* @author Keith Donald
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @since 3.1.2
*/
public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConverter<Object>
@@ -58,6 +61,10 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
// Check for Jackson 2.3's overloaded canDeserialize/canSerialize variants with cause reference
private static final boolean jackson23Available =
ClassUtils.hasMethod(ObjectMapper.class, "canDeserialize", JavaType.class, AtomicReference.class);
private ObjectMapper objectMapper = new ObjectMapper();
@@ -147,12 +154,34 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv
@Override
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
JavaType javaType = getJavaType(type, contextClass);
return (this.objectMapper.canDeserialize(javaType) && canRead(mediaType));
if (!jackson23Available || !logger.isWarnEnabled()) {
return (this.objectMapper.canDeserialize(javaType) && canRead(mediaType));
}
AtomicReference<Throwable> causeRef = new AtomicReference<Throwable>();
if (this.objectMapper.canDeserialize(javaType, causeRef) && canRead(mediaType)) {
return true;
}
Throwable cause = causeRef.get();
if (cause != null) {
logger.warn("Failed to evaluate deserialization for type: " + javaType);
}
return false;
}
@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return (this.objectMapper.canSerialize(clazz) && canWrite(mediaType));
if (!jackson23Available || !logger.isWarnEnabled()) {
return (this.objectMapper.canSerialize(clazz) && canWrite(mediaType));
}
AtomicReference<Throwable> causeRef = new AtomicReference<Throwable>();
if (this.objectMapper.canSerialize(clazz, causeRef) && canWrite(mediaType)) {
return true;
}
Throwable cause = causeRef.get();
if (cause != null) {
logger.warn("Failed to evaluate serialization for type: " + clazz);
}
return false;
}
@Override

View File

@@ -23,10 +23,6 @@ import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.FatalBeanException;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
@@ -45,9 +41,13 @@ import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BasicSerializerFactory;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.fasterxml.jackson.databind.ser.std.ClassSerializer;
import com.fasterxml.jackson.databind.ser.std.NumberSerializers.NumberSerializer;
import com.fasterxml.jackson.databind.ser.std.StdJdkSerializers.ClassSerializer;
import com.fasterxml.jackson.databind.type.SimpleType;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.FatalBeanException;
import static org.junit.Assert.*;
@@ -80,7 +80,7 @@ public class Jackson2ObjectMapperFactoryBeanTests {
@Test(expected = FatalBeanException.class)
public void testUnknownFeature() {
this.factory.setFeaturesToEnable(new Object[] { Boolean.TRUE });
this.factory.setFeaturesToEnable(Boolean.TRUE);
this.factory.afterPropertiesSet();
}
@@ -178,11 +178,11 @@ public class Jackson2ObjectMapperFactoryBeanTests {
assertEquals(ObjectMapper.class, this.factory.getObjectType());
}
private static final SerializerFactoryConfig getSerializerFactoryConfig(ObjectMapper objectMapper) {
private static SerializerFactoryConfig getSerializerFactoryConfig(ObjectMapper objectMapper) {
return ((BasicSerializerFactory) objectMapper.getSerializerFactory()).getFactoryConfig();
}
private static final DeserializerFactoryConfig getDeserializerFactoryConfig(ObjectMapper objectMapper) {
private static DeserializerFactoryConfig getDeserializerFactoryConfig(ObjectMapper objectMapper) {
return ((BasicDeserializerFactory) objectMapper.getDeserializationContext().getFactory()).getFactoryConfig();
}
@@ -221,16 +221,13 @@ public class Jackson2ObjectMapperFactoryBeanTests {
assertFalse(getDeserializerFactoryConfig(objectMapper).hasDeserializers());
this.factory.setSerializationInclusion(JsonInclude.Include.NON_NULL);
this.factory.afterPropertiesSet();
assertTrue(objectMapper == this.factory.getObject());
assertTrue(getSerializerFactoryConfig(objectMapper).hasSerializers());
assertTrue(getDeserializerFactoryConfig(objectMapper).hasDeserializers());
Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
assertTrue(serializers.findSerializer(null, SimpleType.construct(Class.class), null) == serializer1);
assertTrue(serializers.findSerializer(null, SimpleType.construct(Boolean.class), null) == serializer2);
assertNull(serializers.findSerializer(null, SimpleType.construct(Number.class), null));
@@ -247,7 +244,7 @@ public class Jackson2ObjectMapperFactoryBeanTests {
assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
assertFalse(objectMapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
assertFalse(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.QUOTE_FIELD_NAMES));
assertTrue(objectMapper.getSerializationConfig().getSerializationInclusion() == JsonInclude.Include.NON_NULL);
}
}