Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
86c86151
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,发现更多精彩内容 >>
提交
86c86151
编写于
6月 08, 2018
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Accept Predicate instead of HandlerTypePredicate
Issue: SPR-16336
上级
0092653d
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
114 addition
and
37 deletion
+114
-37
spring-web/src/main/java/org/springframework/web/method/HandlerTypePredicate.java
.../org/springframework/web/method/HandlerTypePredicate.java
+9
-4
spring-web/src/test/java/org/springframework/web/method/HandlerTypePredicateTests.java
...springframework/web/method/HandlerTypePredicateTests.java
+66
-0
spring-webflux/src/main/java/org/springframework/web/reactive/config/PathMatchConfigurer.java
...ingframework/web/reactive/config/PathMatchConfigurer.java
+7
-6
spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java
...work/web/reactive/config/WebFluxConfigurationSupport.java
+2
-2
spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerMapping.java
...esult/method/annotation/RequestMappingHandlerMapping.java
+13
-10
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/PathMatchConfigurer.java
...rk/web/servlet/config/annotation/PathMatchConfigurer.java
+7
-6
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java
...servlet/config/annotation/WebMvcConfigurationSupport.java
+2
-2
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java
...t/mvc/method/annotation/RequestMappingHandlerMapping.java
+8
-7
未找到文件。
spring-web/src/main/java/org/springframework/web/method/HandlerTypePredicate.java
浏览文件 @
86c86151
...
...
@@ -29,19 +29,24 @@ import org.springframework.util.ClassUtils;
import
org.springframework.util.StringUtils
;
/**
* A {@code Predicate} to match request handling component types
based on the
*
following selectors
:
* A {@code Predicate} to match request handling component types
if
*
<strong>any</strong> of the following selectors match
:
* <ul>
* <li>Base packages -- for selecting handlers by their package.
* <li>Assignable types -- for selecting handlers by super type.
* <li>Annotations -- for selecting handlers annotated in a specific way.
* </ul>
* <p>Use static factory methods in this class to create a Predicate.
* <p>Composability methods on {@link Predicate} can be used :
* <pre class="code">
* Predicate<Class<?>> predicate =
* HandlerTypePredicate.forAnnotation(RestController)
* .and(HandlerTypePredicate.forBasePackage("org.example"));
* </pre>
*
* @author Rossen Stoyanchev
* @since 5.1
*/
public
class
HandlerTypePredicate
implements
Predicate
<
Class
<?>>
{
public
class
HandlerTypePredicate
implements
Predicate
<
Class
<?>>
{
private
final
Set
<
String
>
basePackages
;
...
...
spring-web/src/test/java/org/springframework/web/method/HandlerTypePredicateTests.java
0 → 100644
浏览文件 @
86c86151
/*
* Copyright 2002-2018 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.web.method
;
import
java.util.function.Predicate
;
import
org.junit.Test
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.RestController
;
import
static
org
.
junit
.
Assert
.*;
/**
* Unit tests for {@link HandlerTypePredicate}.
* @author Rossen Stoyanchev
*/
public
class
HandlerTypePredicateTests
{
@Test
public
void
forAnnotation
()
{
Predicate
<
Class
<?>>
predicate
=
HandlerTypePredicate
.
forAnnotation
(
Controller
.
class
);
assertTrue
(
predicate
.
test
(
HtmlController
.
class
));
assertTrue
(
predicate
.
test
(
ApiController
.
class
));
assertTrue
(
predicate
.
test
(
AnotherApiController
.
class
));
}
@Test
public
void
forAnnotationWithException
()
{
Predicate
<
Class
<?>>
predicate
=
HandlerTypePredicate
.
forAnnotation
(
Controller
.
class
)
.
and
(
HandlerTypePredicate
.
forAssignableType
(
Special
.
class
));
assertFalse
(
predicate
.
test
(
HtmlController
.
class
));
assertFalse
(
predicate
.
test
(
ApiController
.
class
));
assertTrue
(
predicate
.
test
(
AnotherApiController
.
class
));
}
@Controller
private
static
class
HtmlController
{}
@RestController
private
static
class
ApiController
{}
@RestController
private
static
class
AnotherApiController
implements
Special
{}
interface
Special
{}
}
spring-webflux/src/main/java/org/springframework/web/reactive/config/PathMatchConfigurer.java
浏览文件 @
86c86151
...
...
@@ -18,9 +18,9 @@ package org.springframework.web.reactive.config;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
java.util.function.Predicate
;
import
org.springframework.lang.Nullable
;
import
org.springframework.web.method.HandlerTypePredicate
;
/**
* Assist with configuring {@code HandlerMapping}'s with path matching options.
...
...
@@ -39,7 +39,7 @@ public class PathMatchConfigurer {
private
Boolean
caseSensitiveMatch
;
@Nullable
private
Map
<
String
,
HandlerTypePredicate
>
pathPrefixes
;
private
Map
<
String
,
Predicate
<
Class
<?>>
>
pathPrefixes
;
/**
...
...
@@ -66,13 +66,14 @@ public class PathMatchConfigurer {
* Configure a path prefix to apply to matching controller methods.
* <p>Prefixes are used to enrich the mappings of every {@code @RequestMapping}
* method whose controller type is matched by the corresponding
* {@link HandlerTypePredicate}. The prefix for the first matching predicate
* is used.
* {@code Predicate}. The prefix for the first matching predicate is used.
* <p>Consider using {@link org.springframework.web.method.HandlerTypePredicate
* HandlerTypePredicate} to group controllers.
* @param prefix the path prefix to apply
* @param predicate a predicate for matching controller types
* @since 5.1
*/
public
PathMatchConfigurer
addPathPrefix
(
String
prefix
,
HandlerTypePredicate
predicate
)
{
public
PathMatchConfigurer
addPathPrefix
(
String
prefix
,
Predicate
<
Class
<?>>
predicate
)
{
this
.
pathPrefixes
=
this
.
pathPrefixes
==
null
?
new
LinkedHashMap
<>()
:
this
.
pathPrefixes
;
this
.
pathPrefixes
.
put
(
prefix
,
predicate
);
return
this
;
...
...
@@ -89,7 +90,7 @@ public class PathMatchConfigurer {
}
@Nullable
protected
Map
<
String
,
HandlerTypePredicate
>
getPathPrefixes
()
{
protected
Map
<
String
,
Predicate
<
Class
<?>>
>
getPathPrefixes
()
{
return
this
.
pathPrefixes
;
}
}
spring-webflux/src/main/java/org/springframework/web/reactive/config/WebFluxConfigurationSupport.java
浏览文件 @
86c86151
...
...
@@ -18,6 +18,7 @@ package org.springframework.web.reactive.config;
import
java.util.List
;
import
java.util.Map
;
import
java.util.function.Predicate
;
import
reactor.core.publisher.Mono
;
...
...
@@ -44,7 +45,6 @@ import org.springframework.validation.Validator;
import
org.springframework.web.bind.WebDataBinder
;
import
org.springframework.web.bind.support.ConfigurableWebBindingInitializer
;
import
org.springframework.web.cors.CorsConfiguration
;
import
org.springframework.web.method.HandlerTypePredicate
;
import
org.springframework.web.reactive.DispatcherHandler
;
import
org.springframework.web.reactive.HandlerMapping
;
import
org.springframework.web.reactive.accept.RequestedContentTypeResolver
;
...
...
@@ -131,7 +131,7 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware {
mapping
.
setUseCaseSensitiveMatch
(
useCaseSensitiveMatch
);
}
Map
<
String
,
HandlerTypePredicate
>
pathPrefixes
=
configurer
.
getPathPrefixes
();
Map
<
String
,
Predicate
<
Class
<?>>
>
pathPrefixes
=
configurer
.
getPathPrefixes
();
if
(
pathPrefixes
!=
null
)
{
mapping
.
setPathPrefixes
(
pathPrefixes
);
}
...
...
spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerMapping.java
浏览文件 @
86c86151
...
...
@@ -21,6 +21,7 @@ import java.lang.reflect.Method;
import
java.util.Collections
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
java.util.function.Predicate
;
import
org.springframework.context.EmbeddedValueResolverAware
;
import
org.springframework.core.annotation.AnnotatedElementUtils
;
...
...
@@ -34,7 +35,6 @@ import org.springframework.web.bind.annotation.CrossOrigin;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.cors.CorsConfiguration
;
import
org.springframework.web.method.HandlerTypePredicate
;
import
org.springframework.web.method.HandlerMethod
;
import
org.springframework.web.reactive.accept.RequestedContentTypeResolver
;
import
org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder
;
...
...
@@ -53,7 +53,7 @@ import org.springframework.web.reactive.result.method.RequestMappingInfoHandlerM
public
class
RequestMappingHandlerMapping
extends
RequestMappingInfoHandlerMapping
implements
EmbeddedValueResolverAware
{
private
final
Map
<
String
,
HandlerTypePredicate
>
pathPrefixes
=
new
LinkedHashMap
<>();
private
final
Map
<
String
,
Predicate
<
Class
<?>>
>
pathPrefixes
=
new
LinkedHashMap
<>();
private
RequestedContentTypeResolver
contentTypeResolver
=
new
RequestedContentTypeResolverBuilder
().
build
();
...
...
@@ -66,13 +66,16 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
/**
* Configure path prefixes to apply to controller methods.
* <p>Prefixes are used to enrich the mappings of every {@code @RequestMapping}
* method whose controller type is matched by the corresponding
* {@link HandlerTypePredicate} in the map. The prefix for the first matching
* predicate is used, assuming the input map has predictable order.
* method whose controller type is matched by a corresponding
* {@code Predicate} in the map. The prefix for the first matching predicate
* is used, assuming the input map has predictable order.
* <p>Consider using {@link org.springframework.web.method.HandlerTypePredicate
* HandlerTypePredicate} to group controllers.
* @param prefixes a map with path prefixes as key
* @since 5.1
* @see org.springframework.web.method.HandlerTypePredicate
*/
public
void
setPathPrefixes
(
Map
<
String
,
HandlerTypePredicate
>
prefixes
)
{
public
void
setPathPrefixes
(
Map
<
String
,
Predicate
<
Class
<?>>
>
prefixes
)
{
this
.
pathPrefixes
.
clear
();
prefixes
.
entrySet
().
stream
()
.
filter
(
entry
->
StringUtils
.
hasText
(
entry
.
getKey
()))
...
...
@@ -80,8 +83,8 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
}
/**
* Set the {@link RequestedContentTypeResolver} to use to determine requested
media types.
* If not set, the default constructor is used.
* Set the {@link RequestedContentTypeResolver} to use to determine requested
*
media types.
If not set, the default constructor is used.
*/
public
void
setContentTypeResolver
(
RequestedContentTypeResolver
contentTypeResolver
)
{
Assert
.
notNull
(
contentTypeResolver
,
"'contentTypeResolver' must not be null"
);
...
...
@@ -107,7 +110,7 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
* The configured path prefixes as a read-only, possibly empty map.
* @since 5.1
*/
public
Map
<
String
,
HandlerTypePredicate
>
getPathPrefixes
()
{
public
Map
<
String
,
Predicate
<
Class
<?>>
>
getPathPrefixes
()
{
return
Collections
.
unmodifiableMap
(
this
.
pathPrefixes
);
}
...
...
@@ -144,7 +147,7 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
if
(
typeInfo
!=
null
)
{
info
=
typeInfo
.
combine
(
info
);
}
for
(
Map
.
Entry
<
String
,
HandlerTypePredicate
>
entry
:
this
.
pathPrefixes
.
entrySet
())
{
for
(
Map
.
Entry
<
String
,
Predicate
<
Class
<?>>
>
entry
:
this
.
pathPrefixes
.
entrySet
())
{
if
(
entry
.
getValue
().
test
(
handlerType
))
{
String
prefix
=
entry
.
getKey
();
if
(
this
.
embeddedValueResolver
!=
null
)
{
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/PathMatchConfigurer.java
浏览文件 @
86c86151
...
...
@@ -18,10 +18,10 @@ package org.springframework.web.servlet.config.annotation;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
java.util.function.Predicate
;
import
org.springframework.lang.Nullable
;
import
org.springframework.util.PathMatcher
;
import
org.springframework.web.method.HandlerTypePredicate
;
import
org.springframework.web.util.UrlPathHelper
;
/**
...
...
@@ -58,7 +58,7 @@ public class PathMatchConfigurer {
private
PathMatcher
pathMatcher
;
@Nullable
private
Map
<
String
,
HandlerTypePredicate
>
pathPrefixes
;
private
Map
<
String
,
Predicate
<
Class
<?>>
>
pathPrefixes
;
/**
...
...
@@ -121,13 +121,14 @@ public class PathMatchConfigurer {
* Configure a path prefix to apply to matching controller methods.
* <p>Prefixes are used to enrich the mappings of every {@code @RequestMapping}
* method whose controller type is matched by the corresponding
* {@link HandlerTypePredicate}. The prefix for the first matching predicate
* is used.
* {@code Predicate}. The prefix for the first matching predicate is used.
* <p>Consider using {@link org.springframework.web.method.HandlerTypePredicate
* HandlerTypePredicate} to group controllers.
* @param prefix the prefix to apply
* @param predicate a predicate for matching controller types
* @since 5.1
*/
public
PathMatchConfigurer
addPathPrefix
(
String
prefix
,
HandlerTypePredicate
predicate
)
{
public
PathMatchConfigurer
addPathPrefix
(
String
prefix
,
Predicate
<
Class
<?>>
predicate
)
{
this
.
pathPrefixes
=
this
.
pathPrefixes
==
null
?
new
LinkedHashMap
<>()
:
this
.
pathPrefixes
;
this
.
pathPrefixes
.
put
(
prefix
,
predicate
);
return
this
;
...
...
@@ -160,7 +161,7 @@ public class PathMatchConfigurer {
}
@Nullable
protected
Map
<
String
,
HandlerTypePredicate
>
getPathPrefixes
()
{
protected
Map
<
String
,
Predicate
<
Class
<?>>
>
getPathPrefixes
()
{
return
this
.
pathPrefixes
;
}
}
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java
浏览文件 @
86c86151
...
...
@@ -22,6 +22,7 @@ import java.util.HashMap;
import
java.util.List
;
import
java.util.Locale
;
import
java.util.Map
;
import
java.util.function.Predicate
;
import
javax.servlet.ServletContext
;
import
javax.servlet.http.HttpServletRequest
;
...
...
@@ -70,7 +71,6 @@ import org.springframework.web.bind.WebDataBinder;
import
org.springframework.web.bind.support.ConfigurableWebBindingInitializer
;
import
org.springframework.web.context.ServletContextAware
;
import
org.springframework.web.cors.CorsConfiguration
;
import
org.springframework.web.method.HandlerTypePredicate
;
import
org.springframework.web.method.support.CompositeUriComponentsContributor
;
import
org.springframework.web.method.support.HandlerMethodArgumentResolver
;
import
org.springframework.web.method.support.HandlerMethodReturnValueHandler
;
...
...
@@ -311,7 +311,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
mapping
.
setPathMatcher
(
pathMatcher
);
}
Map
<
String
,
HandlerTypePredicate
>
pathPrefixes
=
configurer
.
getPathPrefixes
();
Map
<
String
,
Predicate
<
Class
<?>>
>
pathPrefixes
=
configurer
.
getPathPrefixes
();
if
(
pathPrefixes
!=
null
)
{
mapping
.
setPathPrefixes
(
pathPrefixes
);
}
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java
浏览文件 @
86c86151
...
...
@@ -23,6 +23,7 @@ import java.util.LinkedHashMap;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.function.Predicate
;
import
javax.servlet.http.HttpServletRequest
;
import
org.springframework.context.EmbeddedValueResolverAware
;
...
...
@@ -38,7 +39,6 @@ import org.springframework.web.bind.annotation.CrossOrigin;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.cors.CorsConfiguration
;
import
org.springframework.web.method.HandlerTypePredicate
;
import
org.springframework.web.method.HandlerMethod
;
import
org.springframework.web.servlet.handler.MatchableHandlerMapping
;
import
org.springframework.web.servlet.handler.RequestMatchResult
;
...
...
@@ -67,7 +67,7 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
private
boolean
useTrailingSlashMatch
=
true
;
private
final
Map
<
String
,
HandlerTypePredicate
>
pathPrefixes
=
new
LinkedHashMap
<>();
private
final
Map
<
String
,
Predicate
<
Class
<?>>
>
pathPrefixes
=
new
LinkedHashMap
<>();
private
ContentNegotiationManager
contentNegotiationManager
=
new
ContentNegotiationManager
();
...
...
@@ -113,12 +113,13 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
* Configure path prefixes to apply to controller methods.
* <p>Prefixes are used to enrich the mappings of every {@code @RequestMapping}
* method whose controller type is matched by the corresponding
* {@link HandlerTypePredicate} in the map. The prefix for the first matching
* predicate is used, assuming the input map has predictable order.
* {@code Predicate}. The prefix for the first matching predicate is used.
* <p>Consider using {@link org.springframework.web.method.HandlerTypePredicate
* HandlerTypePredicate} to group controllers.
* @param prefixes a map with path prefixes as key
* @since 5.1
*/
public
void
setPathPrefixes
(
Map
<
String
,
HandlerTypePredicate
>
prefixes
)
{
public
void
setPathPrefixes
(
Map
<
String
,
Predicate
<
Class
<?>>
>
prefixes
)
{
this
.
pathPrefixes
.
clear
();
prefixes
.
entrySet
().
stream
()
.
filter
(
entry
->
StringUtils
.
hasText
(
entry
.
getKey
()))
...
...
@@ -178,7 +179,7 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
* The configured path prefixes as a read-only, possibly empty map.
* @since 5.1
*/
public
Map
<
String
,
HandlerTypePredicate
>
getPathPrefixes
()
{
public
Map
<
String
,
Predicate
<
Class
<?>>
>
getPathPrefixes
()
{
return
Collections
.
unmodifiableMap
(
this
.
pathPrefixes
);
}
...
...
@@ -226,7 +227,7 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
if
(
typeInfo
!=
null
)
{
info
=
typeInfo
.
combine
(
info
);
}
for
(
Map
.
Entry
<
String
,
HandlerTypePredicate
>
entry
:
this
.
pathPrefixes
.
entrySet
())
{
for
(
Map
.
Entry
<
String
,
Predicate
<
Class
<?>>
>
entry
:
this
.
pathPrefixes
.
entrySet
())
{
if
(
entry
.
getValue
().
test
(
handlerType
))
{
String
prefix
=
entry
.
getKey
();
if
(
this
.
embeddedValueResolver
!=
null
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录