Skip to content

Commit a730e55

Browse files
committed
Add support for Jackson 2.7
AbstractJackson2HttpMessageConverter now implements its own TypeVariable resolution algorithm since in Jackson 2.7 it is now deprecated and has not the same behavior . See FasterXML/jackson-databind#1087 for more details. The dependency on jackson-datatype-jdk7 has been removed since it is now provided by default in the jackson-databind module. Issues: SPR-13483, SPR-13728
1 parent ccd17df commit a730e55

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ configure(allprojects) { project ->
4949
ext.htmlunitVersion = "2.19"
5050
ext.httpasyncVersion = "4.1.1"
5151
ext.httpclientVersion = "4.5.1"
52-
ext.jackson2Version = "2.6.4"
52+
ext.jackson2Version = "2.7.0"
5353
ext.jasperreportsVersion = "6.2.0"
5454
ext.javamailVersion = "1.5.5"
5555
ext.jettyVersion = "9.3.6.v20151106"
@@ -735,7 +735,6 @@ project("spring-web") {
735735
exclude group: "org.apache.taglibs", module: "taglibs-standard-spec"
736736
}
737737
testCompile("com.fasterxml.jackson.datatype:jackson-datatype-joda:${jackson2Version}")
738-
testCompile("com.fasterxml.jackson.datatype:jackson-datatype-jdk7:${jackson2Version}")
739738
testCompile("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:${jackson2Version}")
740739
testRuntime("com.sun.mail:javax.mail:${javamailVersion}")
741740
}

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

Lines changed: 33 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.
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.lang.reflect.Type;
21+
import java.lang.reflect.TypeVariable;
2122
import java.nio.charset.Charset;
2223
import java.util.concurrent.atomic.AtomicReference;
2324

@@ -30,7 +31,9 @@
3031
import com.fasterxml.jackson.databind.ObjectWriter;
3132
import com.fasterxml.jackson.databind.SerializationFeature;
3233
import com.fasterxml.jackson.databind.ser.FilterProvider;
34+
import com.fasterxml.jackson.databind.type.TypeFactory;
3335

36+
import org.springframework.core.ResolvableType;
3437
import org.springframework.http.HttpInputMessage;
3538
import org.springframework.http.HttpOutputMessage;
3639
import org.springframework.http.MediaType;
@@ -296,7 +299,35 @@ protected void writeSuffix(JsonGenerator generator, Object object) throws IOExce
296299
* @return the Jackson JavaType
297300
*/
298301
protected JavaType getJavaType(Type type, Class<?> contextClass) {
299-
return this.objectMapper.getTypeFactory().constructType(type, contextClass);
302+
TypeFactory typeFactory = this.objectMapper.getTypeFactory();
303+
if (type instanceof TypeVariable && contextClass != null) {
304+
ResolvableType resolvedType = resolveVariable((TypeVariable<?>)type, ResolvableType.forClass(contextClass));
305+
if (resolvedType != ResolvableType.NONE) {
306+
return typeFactory.constructType(resolvedType.resolve());
307+
}
308+
}
309+
return typeFactory.constructType(type);
310+
}
311+
312+
private ResolvableType resolveVariable(TypeVariable<?> typeVariable, ResolvableType contextType) {
313+
ResolvableType resolvedType;
314+
if (contextType.hasGenerics()) {
315+
resolvedType = ResolvableType.forType(typeVariable, contextType);
316+
if (resolvedType.resolve() != null) {
317+
return resolvedType;
318+
}
319+
}
320+
resolvedType = resolveVariable(typeVariable, contextType.getSuperType());
321+
if (resolvedType.resolve() != null) {
322+
return resolvedType;
323+
}
324+
for (ResolvableType i : contextType.getInterfaces()) {
325+
resolvedType = resolveVariable(typeVariable, i);
326+
if (resolvedType.resolve() != null) {
327+
return resolvedType;
328+
}
329+
}
330+
return ResolvableType.NONE;
300331
}
301332

302333
/**

spring-web/src/test/java/org/springframework/http/converter/json/SpringHandlerInstantiatorTests.java

Lines changed: 7 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.
@@ -218,10 +218,15 @@ public String idFromBaseType() {
218218
return null;
219219
}
220220

221-
// New in Jackson 2.5
221+
@Override
222222
public JavaType typeFromId(DatabindContext context, String id) {
223223
return null;
224224
}
225+
226+
// New in Jackson 2.7
227+
public String getDescForKnownTypeIds() {
228+
return null;
229+
}
225230
}
226231

227232

0 commit comments

Comments
 (0)