|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2015 the original author or authors. |
| 2 | + * Copyright 2002-2016 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
20 | 20 | import java.lang.reflect.Type;
|
| 21 | +import java.lang.reflect.TypeVariable; |
21 | 22 | import java.nio.charset.Charset;
|
22 | 23 | import java.util.concurrent.atomic.AtomicReference;
|
23 | 24 |
|
|
30 | 31 | import com.fasterxml.jackson.databind.ObjectWriter;
|
31 | 32 | import com.fasterxml.jackson.databind.SerializationFeature;
|
32 | 33 | import com.fasterxml.jackson.databind.ser.FilterProvider;
|
| 34 | +import com.fasterxml.jackson.databind.type.TypeFactory; |
33 | 35 |
|
| 36 | +import org.springframework.core.ResolvableType; |
34 | 37 | import org.springframework.http.HttpInputMessage;
|
35 | 38 | import org.springframework.http.HttpOutputMessage;
|
36 | 39 | import org.springframework.http.MediaType;
|
@@ -296,7 +299,35 @@ protected void writeSuffix(JsonGenerator generator, Object object) throws IOExce
|
296 | 299 | * @return the Jackson JavaType
|
297 | 300 | */
|
298 | 301 | 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; |
300 | 331 | }
|
301 | 332 |
|
302 | 333 | /**
|
|
0 commit comments