Spring Boot Webflux:RouterFunctions 添加查询参数

Jos*_*man 6 spring spring-boot spring-webflux

我试图弄清楚如何在使用 RouterFunctions 时将查询参数添加到路由。这是我到目前为止所拥有的:

  @Bean
public RouterFunction<ServerResponse> routes() {
    return
        RouterFunctions.route()
            .GET("/one/{one}", routeHandlerOne::handlerOne)
            .GET("/two", routeHandlerOne::handlerTwo)
        .build();
}
Run Code Online (Sandbox Code Playgroud)

对于路线,two我想添加一个查询参数,例如/two?three. 任何帮助都会非常有帮助,谢谢!

Mic*_*yen 6

queryParam()您可以使用 RequestPredicates 类上的一个方法。

RouterFunctions.route()
   .GET("/one", RequestPredicates.queryParam("test", t -> true), new CustomHanlder())
   .build();
Run Code Online (Sandbox Code Playgroud)

有两个重载方法queryParam()。我们采用精确的值来进行比较(javadoc)。第二个(上例中的那个)采用谓词,如果谓词返回 true ( javadoc ),则将委托给处理程序函数。

ServerRequest然后,您可以通过处理函数中的对象访问查询参数,即。

serverRequest.queryParam("test")
Run Code Online (Sandbox Code Playgroud)