spring - Spring Web Flux(响应式(Reactive))功能路由与Kotlin一起无法正常工作

标签 spring kotlin reactive-programming spring-webflux

您好有兴趣在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 { ...}变体很感兴趣,因为它支持我从另一个位置(如 map 等)添加嵌套路线的用例。

最佳答案

此问题已在SPR-15310中修复。

关于spring - Spring Web Flux(响应式(Reactive))功能路由与Kotlin一起无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42553822/

相关文章:

kotlin - 如何在 ktor 中创建可重用的拦截器?

spring - boundedElastic() 与 parallel() 调度程序之间的区别

java - Spring 集成: retry configuration with multi-instances

hibernate - 具有自定义用户详细信息的 Spring Security

android - 如何在android中共享pdf文件?

rx-java - Rx 等价于 COUNT 和 GROUP BY?

java - RxJava : remove element from list

java - Spring 并在运行时将参数传递给工厂方法

Spring-Boot - 错误处理

Android构建gradle文件版本号自动递增