Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
f380ab9d
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 搜索 >>
提交
f380ab9d
编写于
2月 13, 2017
作者:
S
Sebastien Deleuze
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Polish RouterFunctionExtensions
Issue: SPR-15065
上级
e2e5e765
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
58 addition
and
18 deletion
+58
-18
spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt
.../web/reactive/function/server/RouterFunctionExtensions.kt
+50
-15
spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt
...reactive/function/server/RouterFunctionExtensionsTests.kt
+8
-3
未找到文件。
spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt
浏览文件 @
f380ab9d
...
...
@@ -9,21 +9,33 @@ import reactor.core.publisher.Mono
* Provide a routing DSL for [RouterFunctions] and [RouterFunction] in order to be able to
* write idiomatic Kotlin code as below:
*
* * ```kotlin
* fun route(request: ServerRequest) = route(request) {
* accept(TEXT_HTML).apply {
* (GET("/user/") or GET("/users/")) { findAllView() }
* GET("/user/{login}", ::findViewById)
* }
* accept(APPLICATION_JSON).apply {
* (GET("/api/user/") or GET("/api/users/")) { findAll() }
* POST("/api/user/", ::create)
* POST("/api/user/{login}", ::findOne)
* ```kotlin
* import org.springframework.web.reactive.function.server.RequestPredicates.*
* ...
*
* @Controller
* class FooController : RouterFunction<ServerResponse> {
*
* override fun route(req: ServerRequest) = route(req) {
* html().apply {
* (GET("/user/") or GET("/users/")) { findAllView() }
* GET("/user/{login}", this@FooController::findViewById)
* }
* json().apply {
* (GET("/api/user/") or GET("/api/users/")) { findAll() }
* POST("/api/user/", this@FooController::create)
* }
* }
*
* fun findAllView() = ...
* fun findViewById(req: ServerRequest) = ...
* fun findAll() = ...
* fun create(req: ServerRequest) =
* }
* ```
*
* @since 5.0
* @see <a href="https://youtrack.jetbrains.com/issue/KT-15667">Kotlin issue about supporting ::foo for member functions</a>
* @author Sebastien Deleuze
* @author Yevhenii Melnyk
*/
...
...
@@ -46,7 +58,7 @@ class RouterDsl {
}
fun
GET
(
pattern
:
String
,
f
:
(
ServerRequest
)
->
Mono
<
ServerResponse
>)
{
routes
+=
RouterFunctions
.
route
(
RequestPredicates
.
GET
(
pattern
),
HandlerFunction
{
f
(
it
)
}
)
routes
+=
RouterFunctions
.
route
(
RequestPredicates
.
GET
(
pattern
),
HandlerFunction
{
f
(
it
)
})
}
fun
HEAD
(
pattern
:
String
,
f
:
(
ServerRequest
)
->
Mono
<
ServerResponse
>)
{
...
...
@@ -81,7 +93,7 @@ class RouterDsl {
routes
+=
RouterFunctions
.
route
(
RequestPredicates
.
contentType
(
mediaType
),
HandlerFunction
{
f
(
it
)
})
}
fun
headers
(
headerPredicate
:
(
ServerRequest
.
Headers
)
->
Boolean
,
f
:
(
ServerRequest
)
->
Mono
<
ServerResponse
>)
{
fun
headers
(
headerPredicate
:
(
ServerRequest
.
Headers
)
->
Boolean
,
f
:
(
ServerRequest
)
->
Mono
<
ServerResponse
>)
{
routes
+=
RouterFunctions
.
route
(
RequestPredicates
.
headers
(
headerPredicate
),
HandlerFunction
{
f
(
it
)
})
}
...
...
@@ -93,15 +105,38 @@ class RouterDsl {
routes
+=
RouterFunctions
.
route
(
RequestPredicates
.
path
(
pattern
),
HandlerFunction
{
f
(
it
)
})
}
fun
pathExtension
(
extension
:
String
,
f
:
(
ServerRequest
)
->
Mono
<
ServerResponse
>)
{
routes
+=
RouterFunctions
.
route
(
RequestPredicates
.
pathExtension
(
extension
),
HandlerFunction
{
f
(
it
)
})
}
fun
pathExtension
(
predicate
:
(
String
)
->
Boolean
,
f
:
(
ServerRequest
)
->
Mono
<
ServerResponse
>)
{
routes
+=
RouterFunctions
.
route
(
RequestPredicates
.
pathExtension
(
predicate
),
HandlerFunction
{
f
(
it
)
})
}
fun
queryParam
(
name
:
String
,
predicate
:
(
String
)
->
Boolean
,
f
:
(
ServerRequest
)
->
Mono
<
ServerResponse
>)
{
routes
+=
RouterFunctions
.
route
(
RequestPredicates
.
queryParam
(
name
,
predicate
),
HandlerFunction
{
f
(
it
)
})
}
fun
json
(
f
:
(
ServerRequest
)
->
Mono
<
ServerResponse
>)
{
routes
+=
RouterFunctions
.
route
(
RequestPredicates
.
json
(),
HandlerFunction
{
f
(
it
)
})
}
fun
html
(
f
:
(
ServerRequest
)
->
Mono
<
ServerResponse
>)
{
routes
+=
RouterFunctions
.
route
(
RequestPredicates
.
html
(),
HandlerFunction
{
f
(
it
)
})
}
fun
xml
(
f
:
(
ServerRequest
)
->
Mono
<
ServerResponse
>)
{
routes
+=
RouterFunctions
.
route
(
RequestPredicates
.
xml
(),
HandlerFunction
{
f
(
it
)
})
}
fun
resources
(
path
:
String
,
location
:
Resource
)
{
routes
+=
RouterFunctions
.
resources
(
path
,
location
)
routes
+=
RouterFunctions
.
resources
(
path
,
location
)
}
fun
resources
(
lookupFunction
:
(
ServerRequest
)
->
Mono
<
Resource
>)
{
routes
+=
RouterFunctions
.
resources
(
lookupFunction
)
routes
+=
RouterFunctions
.
resources
(
lookupFunction
)
}
@Suppress
(
"UNCHECKED_CAST"
)
fun
router
():
RouterFunction
<
ServerResponse
>
{
return
routes
().
reduce
(
RouterFunction
<
*
>
::
and
)
as
RouterFunction
<
ServerResponse
>
...
...
spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt
浏览文件 @
f380ab9d
...
...
@@ -97,10 +97,13 @@ class RouterFunctionExtensionsTests {
override
fun
route
(
req
:
ServerRequest
)
=
route
(
req
)
{
(
GET
(
"/foo/"
)
or
GET
(
"/foos/"
))
{
handle
(
req
)
}
accept
(
APPLICATION_JSON
).
apply
{
POST
(
"/api/foo/"
,
::
handle
)
PUT
(
"/api/foo/"
,
::
handle
)
POST
(
"/api/foo/"
,
this
@FooController
::
handleFromClass
)
PUT
(
"/api/foo/"
)
{
handleFromClass
(
req
)
}
DELETE
(
"/api/foo/"
,
::
handle
)
}
html
().
apply
{
GET
(
"/page"
,
this
@FooController
::
handleFromClass
)
}
accept
(
APPLICATION_ATOM_XML
,
::
handle
)
contentType
(
APPLICATION_OCTET_STREAM
)
{
handle
(
req
)
}
method
(
HttpMethod
.
PATCH
)
{
handle
(
req
)
}
...
...
@@ -120,8 +123,10 @@ class RouterFunctionExtensionsTests {
}
path
(
"/baz"
)
{
handle
(
req
)
}
}
fun
handleFromClass
(
req
:
ServerRequest
)
=
ok
().
build
()
}
}
private
fun
handle
(
req
:
ServerRequest
)
=
ok
().
build
()
fun
handle
(
req
:
ServerRequest
)
=
ok
().
build
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录