你好,有兴趣在 Kotlin 中编写 Spring 应用程序的好人。我正在玩 Spring Boot 2.0.0 快照和spring-webflux
. 这段代码:
@Component
class TestRouter() : RouterFunction<ServerResponse> {
override fun route(request: ServerRequest) = route(request) {
"/".route {
GET("/hello") { ServerResponse.ok().body(BodyInserters.fromObject("World")) }
"/{id}".route {
GET("/hello") { ServerResponse.ok().body(BodyInserters.fromObject("World ${request.pathVariable("id")}")) }
}
}
}
}
没有按预期工作(至少如我所料:))
➜ ~ curl -i http://localhost:8080/hello
HTTP/1.1 200 OK
transfer-encoding: chunked
Content-Type: text/plain;charset=UTF-8
World
但:
➜ ~ curl -i http://localhost:8080/1/hello
HTTP/1.1 404 Not Found
content-length: 0
工作案例跟踪:
2017-03-03 00:58:03.865 TRACE 7666 --- [ctor-http-nio-4] o.s.w.r.f.server.RequestPredicates : Pattern "//**" matches against value "/hello"
2017-03-03 00:58:03.865 DEBUG 7666 --- [ctor-http-nio-4] o.s.w.r.function.server.RouterFunctions : Nested predicate "//**" matches against "GET /hello"
2017-03-03 00:58:03.865 TRACE 7666 --- [ctor-http-nio-4] o.s.w.r.f.server.RequestPredicates : Method "GET" matches against value "GET"
2017-03-03 00:58:03.866 TRACE 7666 --- [ctor-http-nio-4] o.s.w.r.f.server.RequestPredicates : Pattern "/hello" matches against value "/hello"
2017-03-03 00:58:03.866 DEBUG 7666 --- [ctor-http-nio-4] o.s.w.r.function.server.RouterFunctions : Predicate "(GET && /hello)" matches against "GET /hello"
不工作案例跟踪:
2017-03-03 00:59:26.958 TRACE 7666 --- [ctor-http-nio-1] o.s.w.r.f.server.RequestPredicates : Pattern "//**" matches against value "/1/hello"
2017-03-03 00:59:26.958 DEBUG 7666 --- [ctor-http-nio-1] o.s.w.r.function.server.RouterFunctions : Nested predicate "//**" matches against "GET /1/hello"
2017-03-03 00:59:26.958 TRACE 7666 --- [ctor-http-nio-1] o.s.w.r.f.server.RequestPredicates : Method "GET" matches against value "GET"
2017-03-03 00:59:26.958 TRACE 7666 --- [ctor-http-nio-1] o.s.w.r.f.server.RequestPredicates : Pattern "/hello" does not match against value "/1/hello"
2017-03-03 00:59:26.959 TRACE 7666 --- [ctor-http-nio-1] o.s.w.r.f.server.RequestPredicates : Pattern "/{id}/**" matches against value "/1/hello"
2017-03-03 00:59:26.959 DEBUG 7666 --- [ctor-http-nio-1] o.s.w.r.function.server.RouterFunctions : Nested predicate "/{id}/**" matches against "GET /1/hello"
2017-03-03 00:59:26.959 TRACE 7666 --- [ctor-http-nio-1] o.s.w.r.f.server.RequestPredicates : Method "GET" matches against value "GET"
2017-03-03 00:59:26.959 TRACE 7666 --- [ctor-http-nio-1] o.s.w.r.f.server.RequestPredicates : Pattern "/hello" does not match against value "/1/hello"
这似乎是一个错误(因为"/{id}".route {...}
据说正在使用 RouterFunctions.nest),但我可能是错的。欢迎您的想法和帮助。
我显然知道我可以/1/hello
通过编写来完成工作GET("/{id}/hello") { ... }
,但我对嵌套.route { ...}
变体感兴趣,因为它支持我从另一个位置(如地图等)添加嵌套路线的用例。