Skip to content

Commit 09db26a

Browse files
committed
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
1 parent 19c5462 commit 09db26a

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -31,6 +31,7 @@
3131
import com.fasterxml.jackson.databind.ObjectWriter;
3232
import com.fasterxml.jackson.databind.SerializationFeature;
3333
import com.fasterxml.jackson.databind.ser.FilterProvider;
34+
import com.fasterxml.jackson.databind.type.TypeFactory;
3435

3536
import org.springframework.http.HttpInputMessage;
3637
import org.springframework.http.HttpOutputMessage;
@@ -48,7 +49,7 @@
4849
* Abstract base class for Jackson based and content type independent
4950
* {@link HttpMessageConverter} implementations.
5051
*
51-
* <p>Compatible with Jackson 2.1 and higher.
52+
* <p>Compatible with Jackson 2.1 to 2.6.
5253
*
5354
* @author Arjen Poutsma
5455
* @author Keith Donald
@@ -308,7 +309,10 @@ protected void writeSuffix(JsonGenerator generator, Object object) throws IOExce
308309
* @return the Jackson JavaType
309310
*/
310311
protected JavaType getJavaType(Type type, Class<?> contextClass) {
311-
return this.objectMapper.getTypeFactory().constructType(type, contextClass);
312+
TypeFactory tf = this.objectMapper.getTypeFactory();
313+
// Conditional call because Jackson 2.7 does not support null contextClass anymore
314+
// TypeVariable resolution will not work with Jackson 2.7, see SPR-13853 for more details
315+
return (contextClass != null ? tf.constructType(type, contextClass) : tf.constructType(type));
312316
}
313317

314318
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -35,7 +35,7 @@
3535
*
3636
* <p>The default constructor uses the default configuration provided by {@link Jackson2ObjectMapperBuilder}.
3737
*
38-
* <p>Compatible with Jackson 2.1 and higher.
38+
* <p>Compatible with Jackson 2.1 to 2.6.
3939
*
4040
* @author Arjen Poutsma
4141
* @author Keith Donald

spring-web/src/main/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -35,7 +35,7 @@
3535
*
3636
* <p>The default constructor uses the default configuration provided by {@link Jackson2ObjectMapperBuilder}.
3737
*
38-
* <p>Compatible with Jackson 2.1 and higher.
38+
* <p>Compatible with Jackson 2.1 to 2.6.
3939
*
4040
* @author Sebastien Deleuze
4141
* @since 4.1

0 commit comments

Comments
 (0)