From 09db26af2b92457fc100c955456486c0a95001a8 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Thu, 11 Feb 2016 08:51:42 +0100 Subject: [PATCH] Improve Jackson 2.7 compatibility This commit introduces a conditional call in AbstractJackson2HttpMessageConverter#getJavaType() in order to avoid calling TypeFactory#constructType() with a null contextClass parameter, since this is not supported by Jackson 2.7 anymore. It allows to use Spring Framework 4.2.x with Jackson 2.7.1+ for most use cases, but with some limitations. For example, with Jackson 2.7 TypeVariable resolution from method parameters does not work. As a consequence, after this commit Spring Framework 4.2.x still fully supports Jackson up to version 2.6, but improves Jackson 2.7 compatibility. Full support for Jackson 2.7 is provided as of Spring Framework 4.3.x. Issue: SPR-13853 --- .../json/AbstractJackson2HttpMessageConverter.java | 10 +++++++--- .../json/MappingJackson2HttpMessageConverter.java | 4 ++-- .../xml/MappingJackson2XmlHttpMessageConverter.java | 4 ++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java index d648d0d070..04a4b59ac8 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -31,6 +31,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.ser.FilterProvider; +import com.fasterxml.jackson.databind.type.TypeFactory; import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; @@ -48,7 +49,7 @@ import org.springframework.util.TypeUtils; * Abstract base class for Jackson based and content type independent * {@link HttpMessageConverter} implementations. * - *

Compatible with Jackson 2.1 and higher. + *

Compatible with Jackson 2.1 to 2.6. * * @author Arjen Poutsma * @author Keith Donald @@ -308,7 +309,10 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener * @return the Jackson JavaType */ protected JavaType getJavaType(Type type, Class contextClass) { - return this.objectMapper.getTypeFactory().constructType(type, contextClass); + TypeFactory tf = this.objectMapper.getTypeFactory(); + // Conditional call because Jackson 2.7 does not support null contextClass anymore + // TypeVariable resolution will not work with Jackson 2.7, see SPR-13853 for more details + return (contextClass != null ? tf.constructType(type, contextClass) : tf.constructType(type)); } /** diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.java index 43f2eba97f..e2ef12b935 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -35,7 +35,7 @@ import org.springframework.http.MediaType; * *

The default constructor uses the default configuration provided by {@link Jackson2ObjectMapperBuilder}. * - *

Compatible with Jackson 2.1 and higher. + *

Compatible with Jackson 2.1 to 2.6. * * @author Arjen Poutsma * @author Keith Donald diff --git a/spring-web/src/main/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverter.java index 2f5aaffc10..03084759b4 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -35,7 +35,7 @@ import org.springframework.util.Assert; * *

The default constructor uses the default configuration provided by {@link Jackson2ObjectMapperBuilder}. * - *

Compatible with Jackson 2.1 and higher. + *

Compatible with Jackson 2.1 to 2.6. * * @author Sebastien Deleuze * @since 4.1