Webflux 在 root 上嵌套路由器总是 returns 404

Webflux nested router on root always returns 404

我有一个带有嵌套路由的 RouterFunction,除了一条路由之外的所有内容都是我认为它们应该做的。
但是当我尝试调用嵌套路由中的根路由之一时,我总是得到 404。这只发生在该特定路由上,当我将它从根更改为“/foo”时它开始工作。

代码:

    public RouterFunction<ServerResponse> productRouter(final ProductHandler handler) {
        return nest(path(apiPath + BASE_PATH),
                route(POST("/").and(contentType(MediaType.APPLICATION_JSON)), handler::handleCreateProduct)
                        .andRoute(GET("/{id}"), handler::handleGetProductById)
                        .andRoute(PUT("/").and(contentType(MediaType.APPLICATION_JSON)), handler::handleUpdateProduct)
                        .andRoute(GET("/"), handler::handleGetAllProducts)
                        .andNest(path("/category"),
                                route(POST("/").and(contentType(MediaType.APPLICATION_JSON)), handler::handleCreateProductCategory)
                                        .andRoute(GET("/{id}"), handler::handleGetProductCategoryById)
                                        .andRoute(GET("/"), handler::handleGetAllProductCategories)
                                        .andRoute(GET("/search/{name}"), handler::handleGetProductCategoriesByName)
                        ))
                .andNest(path("/brand"),
                        route(POST("/").and(contentType(MediaType.APPLICATION_JSON)), handler::handleCreateProductBrand)
                                .andRoute(GET("/"), handler::handleGetAllProductBrands)
                                .andRoute(GET("/{id}"), handler::handleGetProductBrandById));
    }

不正确的路线如下:

.andRoute(GET("/"), handler::handleGetAllProductCategories)

奇怪的是在根路径和品牌路径下我做了完全相同的事情并且这些路由有效。

感谢您的帮助

现在 Weblux 中有一个关于映射根元素的未解决的错误:https://github.com/spring-projects/spring-boot/issues/9785

您应该使用重定向或不使用“/”映射。

我没能在 Spring Boot 2.1 上重现这个问题。2.RELEASE,如下:

@Configuration
public class RouterConfig {

    @Bean
    public RouterFunction<ServerResponse> productRouter() {
        return nest(path("/test"),
                route(GET("/"), serverRequest -> ServerResponse.ok().syncBody("TEST"))
                        .andNest(path("/other"),
                                route(GET("/{id}"), serverRequest -> ServerResponse.ok().syncBody("ID"))
                                .andRoute(GET("/"), serverRequest -> ServerResponse.ok().syncBody("OTHER"))));
    }
}

我得到了结果:

➜  ~ http :8080/test
HTTP/1.1 200 OK
Content-Length: 4
Content-Type: text/plain;charset=UTF-8

TEST

➜  ~ http :8080/test/
HTTP/1.1 200 OK
Content-Length: 4
Content-Type: text/plain;charset=UTF-8

TEST

➜  ~ http :8080/test/other
HTTP/1.1 200 OK
Content-Length: 5
Content-Type: text/plain;charset=UTF-8

OTHER

➜  ~ http :8080/test/other/
HTTP/1.1 200 OK
Content-Length: 5
Content-Type: text/plain;charset=UTF-8

OTHER

➜  ~ http :8080/test/other/something
HTTP/1.1 200 OK
Content-Length: 2
Content-Type: text/plain;charset=UTF-8

ID

如果您设法在示例应用程序中重现此问题,请在 Spring Framework issue tracker 上创建一个问题,因为我没有设法找到该问题的现有问题。请在那里提供一个示例项目,我们可以克隆并 运行 重现问题。

多亏了 Brian Clozel 的评论,我才弄明白了

keep in mind that router functions are about the first route that matches.

考虑到这一点,我按以下方式重组了我的 RouterFunctions:

public RouterFunction<ServerResponse> productRouter(final ProductHandler handler) {

        return nest(path(apiPath + BASE_PATH),
                route(method(HttpMethod.POST).and(accept(MediaType.MULTIPART_FORM_DATA)), handler::handleCreateProduct)
                        .andNest(path("/category"),
                                route(GET("/{id}"), handler::handleGetProductCategoryById)
                                        .andRoute(method(HttpMethod.POST).and(contentType(MediaType.APPLICATION_JSON)), handler::handleCreateProductCategory)
                                        .andRoute(GET("/search/{name}"), handler::handleGetProductCategoriesByName)
                                        .andRoute(method(HttpMethod.GET), handler::handleGetAllProductCategories))
                        .andNest(path("/brand"),
                                route(method(HttpMethod.POST).and(contentType(MediaType.APPLICATION_JSON)), handler::handleCreateProductBrand)
                                        .andRoute(GET("/{id}"), handler::handleGetProductBrandById)
                                        .andRoute(method(HttpMethod.GET), handler::handleGetAllProductBrands))
                        .andRoute(GET("/{id}/pdf"), handler::handlaProductDetailsPdf)
                        .andRoute(GET("/{id}"), handler::handleGetProductById)
                        .andRoute(method(HttpMethod.PUT).and(contentType(MediaType.APPLICATION_JSON)), handler::handleUpdateProduct)
                        .andRoute(method(HttpMethod.GET), handler::handleGetAllProducts));
}

我已将链中的 /category 和 /brand 路径移动到比根路径更高的位置,并且它按预期工作。

再次感谢 Brian 的帮助!