diff --git a/build.gradle b/build.gradle index 622fa09aa6..69cbc8a183 100644 --- a/build.gradle +++ b/build.gradle @@ -49,7 +49,7 @@ configure(allprojects) { project -> ext.htmlunitVersion = "2.19" ext.httpasyncVersion = "4.1.1" ext.httpclientVersion = "4.5.1" - ext.jackson2Version = "2.6.4" + ext.jackson2Version = "2.7.0" ext.jasperreportsVersion = "6.2.0" ext.javamailVersion = "1.5.5" ext.jettyVersion = "9.3.6.v20151106" @@ -735,7 +735,6 @@ project("spring-web") { exclude group: "org.apache.taglibs", module: "taglibs-standard-spec" } testCompile("com.fasterxml.jackson.datatype:jackson-datatype-joda:${jackson2Version}") - testCompile("com.fasterxml.jackson.datatype:jackson-datatype-jdk7:${jackson2Version}") testCompile("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:${jackson2Version}") testRuntime("com.sun.mail:javax.mail:${javamailVersion}") } 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 fb0bbc67a4..f844bee510 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. @@ -18,6 +18,7 @@ package org.springframework.http.converter.json; import java.io.IOException; import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; import java.nio.charset.Charset; import java.util.concurrent.atomic.AtomicReference; @@ -30,7 +31,9 @@ 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.core.ResolvableType; import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; @@ -296,7 +299,35 @@ 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 typeFactory = this.objectMapper.getTypeFactory(); + if (type instanceof TypeVariable && contextClass != null) { + ResolvableType resolvedType = resolveVariable((TypeVariable)type, ResolvableType.forClass(contextClass)); + if (resolvedType != ResolvableType.NONE) { + return typeFactory.constructType(resolvedType.resolve()); + } + } + return typeFactory.constructType(type); + } + + private ResolvableType resolveVariable(TypeVariable typeVariable, ResolvableType contextType) { + ResolvableType resolvedType; + if (contextType.hasGenerics()) { + resolvedType = ResolvableType.forType(typeVariable, contextType); + if (resolvedType.resolve() != null) { + return resolvedType; + } + } + resolvedType = resolveVariable(typeVariable, contextType.getSuperType()); + if (resolvedType.resolve() != null) { + return resolvedType; + } + for (ResolvableType i : contextType.getInterfaces()) { + resolvedType = resolveVariable(typeVariable, i); + if (resolvedType.resolve() != null) { + return resolvedType; + } + } + return ResolvableType.NONE; } /** diff --git a/spring-web/src/test/java/org/springframework/http/converter/json/SpringHandlerInstantiatorTests.java b/spring-web/src/test/java/org/springframework/http/converter/json/SpringHandlerInstantiatorTests.java index 687df9c993..b9000b736e 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/json/SpringHandlerInstantiatorTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/json/SpringHandlerInstantiatorTests.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. @@ -218,10 +218,15 @@ public class SpringHandlerInstantiatorTests { return null; } - // New in Jackson 2.5 + @Override public JavaType typeFromId(DatabindContext context, String id) { return null; } + + // New in Jackson 2.7 + public String getDescForKnownTypeIds() { + return null; + } }