繁体   English   中英

在 ServerResponse 中将 mono 作为 header 值传递

[英]Pass mono as a header value in ServerResponse

我是功能端点/spring webflux 的新手,我正在尝试将 mono 作为 header 值返回,但我找不到这样做的方法,因为header只接受字符串。

路由器function,

@Bean
RouterFunction<ServerResponse> productRoutes() {
    return route(GET("/products").and(accept(MediaType.APPLICATION_JSON)), getAllProductsHandler());
}

处理程序 function,

private HandlerFunction<ServerResponse> getAllProductsHandler() {
    return req -> {

        // Here the products and the totalProducts are being returned from the service layer 

        Flux<Product> products = Flux.just(new Product("123"), new Product("234"));
        Mono<Integer> totalProducts = Mono.just(2);

        return ok()
                .header("totalCount", totalProducts)
                .body(products, Product.class);
    };
}

在这里将 mono 作为 header 值返回的正确方法是什么?

通过链接 mono 并构建您的响应。

final Mono<Integer> totalProducts = Mono.just(2);

return totalProducts.flatMap(value -> ok()
                .header("totalCount", value)
                .body(products, Product.class)
        );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM