Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
3a4e5d5d
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3a4e5d5d
编写于
7月 19, 2016
作者:
S
Sebastien Deleuze
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix ParameterizedType + contextClass support in Jackson converter
Issue: SPR-14470
上级
b52b56c9
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
66 addition
and
5 deletion
+66
-5
spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java
.../converter/json/AbstractJackson2HttpMessageConverter.java
+32
-5
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java
...d/annotation/RequestResponseBodyMethodProcessorTests.java
+34
-0
未找到文件。
spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java
浏览文件 @
3a4e5d5d
...
...
@@ -17,6 +17,7 @@
package
org.springframework.http.converter.json
;
import
java.io.IOException
;
import
java.lang.reflect.ParameterizedType
;
import
java.lang.reflect.Type
;
import
java.lang.reflect.TypeVariable
;
import
java.nio.charset.Charset
;
...
...
@@ -312,11 +313,37 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener
*/
protected
JavaType
getJavaType
(
Type
type
,
Class
<?>
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
());
if
(
contextClass
!=
null
)
{
ResolvableType
resolvedType
=
ResolvableType
.
forType
(
type
);
if
(
type
instanceof
TypeVariable
)
{
ResolvableType
resolvedTypeVariable
=
resolveVariable
(
(
TypeVariable
<?>)
type
,
ResolvableType
.
forClass
(
contextClass
));
if
(
resolvedTypeVariable
!=
ResolvableType
.
NONE
)
{
return
typeFactory
.
constructType
(
resolvedTypeVariable
.
resolve
());
}
}
else
if
(
type
instanceof
ParameterizedType
&&
resolvedType
.
hasUnresolvableGenerics
())
{
ParameterizedType
parameterizedType
=
(
ParameterizedType
)
type
;
Class
<?>[]
generics
=
new
Class
<?>[
parameterizedType
.
getActualTypeArguments
().
length
];
Type
[]
typeArguments
=
parameterizedType
.
getActualTypeArguments
();
for
(
int
i
=
0
;
i
<
typeArguments
.
length
;
i
++)
{
Type
typeArgument
=
typeArguments
[
i
];
if
(
typeArgument
instanceof
TypeVariable
)
{
ResolvableType
resolvedTypeArgument
=
resolveVariable
(
(
TypeVariable
<?>)
typeArgument
,
ResolvableType
.
forClass
(
contextClass
));
if
(
resolvedTypeArgument
!=
ResolvableType
.
NONE
)
{
generics
[
i
]
=
resolvedTypeArgument
.
resolve
();
}
else
{
generics
[
i
]
=
ResolvableType
.
forType
(
typeArgument
).
resolve
();
}
}
else
{
generics
[
i
]
=
ResolvableType
.
forType
(
typeArgument
).
resolve
();
}
}
return
typeFactory
.
constructType
(
ResolvableType
.
forClassWithGenerics
(
resolvedType
.
getRawClass
(),
generics
).
getType
());
}
}
return
typeFactory
.
constructType
(
type
);
...
...
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java
浏览文件 @
3a4e5d5d
...
...
@@ -240,6 +240,29 @@ public class RequestResponseBodyMethodProcessorTests {
assertEquals
(
"Jad"
,
result
.
getName
());
}
@Test
// SPR-14470
public
void
resolveParameterizedWithTypeVariableArgument
()
throws
Exception
{
Method
method
=
MyParameterizedControllerWithList
.
class
.
getMethod
(
"handleDto"
,
List
.
class
);
HandlerMethod
handlerMethod
=
new
HandlerMethod
(
new
MySimpleParameterizedControllerWithList
(),
method
);
MethodParameter
methodParam
=
handlerMethod
.
getMethodParameters
()[
0
];
String
content
=
"[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]"
;
this
.
servletRequest
.
setContent
(
content
.
getBytes
(
"UTF-8"
));
this
.
servletRequest
.
setContentType
(
MediaType
.
APPLICATION_JSON_VALUE
);
List
<
HttpMessageConverter
<?>>
converters
=
new
ArrayList
<>();
converters
.
add
(
new
MappingJackson2HttpMessageConverter
());
RequestResponseBodyMethodProcessor
processor
=
new
RequestResponseBodyMethodProcessor
(
converters
);
@SuppressWarnings
(
"unchecked"
)
List
<
SimpleBean
>
result
=
(
List
<
SimpleBean
>)
processor
.
resolveArgument
(
methodParam
,
container
,
request
,
factory
);
assertNotNull
(
result
);
assertEquals
(
"Jad"
,
result
.
get
(
0
).
getName
());
assertEquals
(
"Robert"
,
result
.
get
(
1
).
getName
());
}
@Test
// SPR-11225
public
void
resolveArgumentTypeVariableWithNonGenericConverter
()
throws
Exception
{
Method
method
=
MyParameterizedController
.
class
.
getMethod
(
"handleDto"
,
Identifiable
.
class
);
...
...
@@ -725,6 +748,17 @@ public class RequestResponseBodyMethodProcessorTests {
void
setId
(
Long
id
);
}
@SuppressWarnings
(
"unused"
)
private
static
abstract
class
MyParameterizedControllerWithList
<
DTO
extends
Identifiable
>
{
public
void
handleDto
(
@RequestBody
List
<
DTO
>
dto
)
{
}
}
@SuppressWarnings
(
"unused"
)
private
static
class
MySimpleParameterizedControllerWithList
extends
MyParameterizedControllerWithList
<
SimpleBean
>
{
}
@SuppressWarnings
({
"serial"
})
private
static
class
SimpleBean
implements
Identifiable
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录