Reputation: 740
I'm new to functional endpoints/spring webflux and I'm trying to return mono as a header value but I couldn't find a way to do it as the header
method accepts only the string.
Router function,
@Bean
RouterFunction<ServerResponse> productRoutes() {
return route(GET("/products").and(accept(MediaType.APPLICATION_JSON)), getAllProductsHandler());
}
Handler 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);
};
}
What is the right way to return mono as a header value here?
Upvotes: 1
Views: 1017
Reputation: 14819
By chaining on the mono and build your response.
final Mono<Integer> totalProducts = Mono.just(2);
return totalProducts.flatMap(value -> ok()
.header("totalCount", value)
.body(products, Product.class)
);
Upvotes: 2