Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
5a3ff352
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,体验更适合开发者的 AI 搜索 >>
提交
5a3ff352
编写于
3月 19, 2019
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Map arg resolver backs out if annotations present
Closes gh-21874
上级
a2fcf0a8
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
71 addition
and
44 deletion
+71
-44
spring-web/src/main/java/org/springframework/web/method/annotation/MapMethodProcessor.java
...ngframework/web/method/annotation/MapMethodProcessor.java
+5
-4
spring-web/src/test/java/org/springframework/web/method/annotation/MapMethodProcessorTests.java
...mework/web/method/annotation/MapMethodProcessorTests.java
+30
-20
spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelArgumentResolver.java
...ctive/result/method/annotation/ModelArgumentResolver.java
+10
-4
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ModelArgumentResolverTests.java
.../result/method/annotation/ModelArgumentResolverTests.java
+26
-16
未找到文件。
spring-web/src/main/java/org/springframework/web/method/annotation/MapMethodProcessor.java
浏览文件 @
5a3ff352
/*
* Copyright 2002-201
8
the original author or authors.
* Copyright 2002-201
9
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.
...
...
@@ -32,8 +32,8 @@ import org.springframework.web.method.support.ModelAndViewContainer;
*
* <p>A Map return value can be interpreted in more than one ways depending
* on the presence of annotations like {@code @ModelAttribute} or
* {@code @ResponseBody}.
Therefore this handler should be configured after
*
the handlers that support these annotations
.
* {@code @ResponseBody}.
As of 5.2 this resolver returns false if the
*
parameter is annotated
.
*
* @author Rossen Stoyanchev
* @since 3.1
...
...
@@ -42,7 +42,8 @@ public class MapMethodProcessor implements HandlerMethodArgumentResolver, Handle
@Override
public
boolean
supportsParameter
(
MethodParameter
parameter
)
{
return
Map
.
class
.
isAssignableFrom
(
parameter
.
getParameterType
());
return
Map
.
class
.
isAssignableFrom
(
parameter
.
getParameterType
())
&&
parameter
.
getParameterAnnotations
().
length
==
0
;
}
@Override
...
...
spring-web/src/test/java/org/springframework/web/method/annotation/MapMethodProcessorTests.java
浏览文件 @
5a3ff352
/*
* Copyright 2002-201
8
the original author or authors.
* Copyright 2002-201
9
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.
...
...
@@ -16,7 +16,6 @@
package
org.springframework.web.method.annotation
;
import
java.lang.reflect.Method
;
import
java.util.Map
;
import
org.junit.Before
;
...
...
@@ -25,14 +24,18 @@ import org.junit.Test;
import
org.springframework.core.MethodParameter
;
import
org.springframework.mock.web.test.MockHttpServletRequest
;
import
org.springframework.ui.ModelMap
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.context.request.NativeWebRequest
;
import
org.springframework.web.context.request.ServletWebRequest
;
import
org.springframework.web.method.ResolvableMethod
;
import
org.springframework.web.method.support.ModelAndViewContainer
;
import
static
org
.
junit
.
Assert
.*;
/**
* Test fixture with {@link org.springframework.web.method.annotation.MapMethodProcessor}.
* Test fixture with
* {@link org.springframework.web.method.annotation.MapMethodProcessor}.
*
* @author Rossen Stoyanchev
*/
...
...
@@ -42,52 +45,59 @@ public class MapMethodProcessorTests {
private
ModelAndViewContainer
mavContainer
;
private
MethodParameter
paramMap
;
private
NativeWebRequest
webRequest
;
private
MethodParameter
returnParamMap
;
private
final
ResolvableMethod
resolvable
=
ResolvableMethod
.
on
(
getClass
()).
annotPresent
(
RequestMapping
.
class
).
build
();
private
NativeWebRequest
webRequest
;
@Before
public
void
setUp
()
throws
Exception
{
processor
=
new
MapMethodProcessor
();
mavContainer
=
new
ModelAndViewContainer
();
Method
method
=
getClass
().
getDeclaredMethod
(
"map"
,
Map
.
class
);
paramMap
=
new
MethodParameter
(
method
,
0
);
returnParamMap
=
new
MethodParameter
(
method
,
0
);
webRequest
=
new
ServletWebRequest
(
new
MockHttpServletRequest
());
this
.
processor
=
new
MapMethodProcessor
();
this
.
mavContainer
=
new
ModelAndViewContainer
();
this
.
webRequest
=
new
ServletWebRequest
(
new
MockHttpServletRequest
());
}
@Test
public
void
supportsParameter
()
{
assertTrue
(
processor
.
supportsParameter
(
paramMap
));
assertTrue
(
this
.
processor
.
supportsParameter
(
this
.
resolvable
.
annotNotPresent
().
arg
(
Map
.
class
,
String
.
class
,
Object
.
class
)));
assertFalse
(
this
.
processor
.
supportsParameter
(
this
.
resolvable
.
annotPresent
(
RequestBody
.
class
).
arg
(
Map
.
class
,
String
.
class
,
Object
.
class
)));
}
@Test
public
void
supportsReturnType
()
{
assertTrue
(
processor
.
supportsReturnType
(
returnParamMap
));
assertTrue
(
this
.
processor
.
supportsReturnType
(
this
.
resolvable
.
returnType
()
));
}
@Test
public
void
resolveArgumentValue
()
throws
Exception
{
assertSame
(
mavContainer
.
getModel
(),
processor
.
resolveArgument
(
paramMap
,
mavContainer
,
webRequest
,
null
));
MethodParameter
param
=
this
.
resolvable
.
annotNotPresent
().
arg
(
Map
.
class
,
String
.
class
,
Object
.
class
);
assertSame
(
this
.
mavContainer
.
getModel
(),
this
.
processor
.
resolveArgument
(
param
,
this
.
mavContainer
,
this
.
webRequest
,
null
));
}
@Test
public
void
handleMapReturnValue
()
throws
Exception
{
mavContainer
.
addAttribute
(
"attr1"
,
"value1"
);
this
.
mavContainer
.
addAttribute
(
"attr1"
,
"value1"
);
Map
<
String
,
Object
>
returnValue
=
new
ModelMap
(
"attr2"
,
"value2"
);
processor
.
handleReturnValue
(
returnValue
,
returnParamMap
,
mavContainer
,
webRequest
);
this
.
processor
.
handleReturnValue
(
returnValue
,
this
.
resolvable
.
returnType
(),
this
.
mavContainer
,
this
.
webRequest
);
assertEquals
(
"value1"
,
mavContainer
.
getModel
().
get
(
"attr1"
));
assertEquals
(
"value2"
,
mavContainer
.
getModel
().
get
(
"attr2"
));
}
@SuppressWarnings
(
"unused"
)
private
Map
<
String
,
Object
>
map
(
Map
<
String
,
Object
>
map
)
{
@RequestMapping
private
Map
<
String
,
Object
>
handle
(
Map
<
String
,
Object
>
map
,
@RequestBody
Map
<
String
,
Object
>
annotMap
)
{
return
null
;
}
...
...
spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelArgumentResolver.java
浏览文件 @
5a3ff352
/*
* Copyright 2002-201
8
the original author or authors.
* Copyright 2002-201
9
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.
...
...
@@ -30,6 +30,11 @@ import org.springframework.web.server.ServerWebExchange;
* Resolver for a controller method argument of type {@link Model} that can
* also be resolved as a {@link java.util.Map}.
*
* <p>A Map return value can be interpreted in more than one ways depending
* on the presence of annotations like {@code @ModelAttribute} or
* {@code @ResponseBody}. As of 5.2 this resolver returns false if a
* parameter of type {@code Map} is also annotated.
*
* @author Rossen Stoyanchev
* @since 5.0
*/
...
...
@@ -42,9 +47,10 @@ public class ModelArgumentResolver extends HandlerMethodArgumentResolverSupport
@Override
public
boolean
supportsParameter
(
MethodParameter
parameter
)
{
return
checkParameterTypeNoReactiveWrapper
(
parameter
,
type
->
Model
.
class
.
isAssignableFrom
(
type
)
||
Map
.
class
.
isAssignableFrom
(
type
));
public
boolean
supportsParameter
(
MethodParameter
param
)
{
return
checkParameterTypeNoReactiveWrapper
(
param
,
type
->
Model
.
class
.
isAssignableFrom
(
type
)
||
(
Map
.
class
.
isAssignableFrom
(
type
)
&&
param
.
getParameterAnnotations
().
length
==
0
));
}
@Override
...
...
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ModelArgumentResolverTests.java
浏览文件 @
5a3ff352
/*
* Copyright 2002-201
7
the original author or authors.
* Copyright 2002-201
9
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.
...
...
@@ -25,14 +25,13 @@ import org.springframework.core.ReactiveAdapterRegistry;
import
org.springframework.mock.web.test.server.MockServerWebExchange
;
import
org.springframework.ui.Model
;
import
org.springframework.ui.ModelMap
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.method.ResolvableMethod
;
import
org.springframework.web.reactive.BindingContext
;
import
org.springframework.web.server.ServerWebExchange
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertSame
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
springframework
.
mock
.
http
.
server
.
reactive
.
test
.
MockServerHttpRequest
.
get
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
springframework
.
mock
.
http
.
server
.
reactive
.
test
.
MockServerHttpRequest
.*;
/**
* Unit tests for {@link ModelArgumentResolver}.
...
...
@@ -45,22 +44,27 @@ public class ModelArgumentResolverTests {
private
final
ServerWebExchange
exchange
=
MockServerWebExchange
.
from
(
get
(
"/"
));
private
final
ResolvableMethod
testMethod
=
ResolvableMethod
.
on
(
getClass
()).
named
(
"handle"
).
build
();
private
final
ResolvableMethod
resolvable
=
ResolvableMethod
.
on
(
getClass
()).
named
(
"handle"
).
build
();
@Test
public
void
supportsParameter
()
throws
Exception
{
assertTrue
(
this
.
resolver
.
supportsParameter
(
this
.
testMethod
.
arg
(
Model
.
class
)));
assertTrue
(
this
.
resolver
.
supportsParameter
(
this
.
testMethod
.
arg
(
Map
.
class
,
String
.
class
,
Object
.
class
)));
assertTrue
(
this
.
resolver
.
supportsParameter
(
this
.
testMethod
.
arg
(
ModelMap
.
class
)));
assertFalse
(
this
.
resolver
.
supportsParameter
(
this
.
testMethod
.
arg
(
Object
.
class
)));
public
void
supportsParameter
()
{
assertTrue
(
this
.
resolver
.
supportsParameter
(
this
.
resolvable
.
arg
(
Model
.
class
)));
assertTrue
(
this
.
resolver
.
supportsParameter
(
this
.
resolvable
.
arg
(
ModelMap
.
class
)));
assertTrue
(
this
.
resolver
.
supportsParameter
(
this
.
resolvable
.
annotNotPresent
().
arg
(
Map
.
class
,
String
.
class
,
Object
.
class
)));
assertFalse
(
this
.
resolver
.
supportsParameter
(
this
.
resolvable
.
arg
(
Object
.
class
)));
assertFalse
(
this
.
resolver
.
supportsParameter
(
this
.
resolvable
.
annotPresent
(
RequestBody
.
class
).
arg
(
Map
.
class
,
String
.
class
,
Object
.
class
)));
}
@Test
public
void
resolveArgument
()
throws
Exception
{
testResolveArgument
(
this
.
testMethod
.
arg
(
Model
.
class
));
testResolveArgument
(
this
.
testMethod
.
arg
(
Map
.
class
,
String
.
class
,
Object
.
class
));
testResolveArgument
(
this
.
testMethod
.
arg
(
ModelMap
.
class
));
public
void
resolveArgument
()
{
testResolveArgument
(
this
.
resolvable
.
arg
(
Model
.
class
));
testResolveArgument
(
this
.
resolvable
.
annotNotPresent
()
.
arg
(
Map
.
class
,
String
.
class
,
Object
.
class
));
testResolveArgument
(
this
.
resolvable
.
arg
(
ModelMap
.
class
));
}
private
void
testResolveArgument
(
MethodParameter
parameter
)
{
...
...
@@ -69,7 +73,13 @@ public class ModelArgumentResolverTests {
assertSame
(
context
.
getModel
(),
result
);
}
@SuppressWarnings
(
"unused"
)
void
handle
(
Model
model
,
Map
<
String
,
Object
>
map
,
ModelMap
modelMap
,
Object
object
)
{}
void
handle
(
Model
model
,
Map
<
String
,
Object
>
map
,
@RequestBody
Map
<
String
,
Object
>
annotatedMap
,
ModelMap
modelMap
,
Object
object
)
{}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录