Returns a object from RouterFunction handlers in Spring 5 WebFlux

Returns a object from RouterFunction handlers in Spring 5 WebFlux

spring webflux tutorial
spring webflux webclient
spring webflux crud example
spring webflux performance
spring webflux functional endpoints
spring webflux vs spring mvc
spring webflux logging
scanavailable'': true

I have the following bean definition for a RouterFunction:

@Bean
    RouterFunction<?> routes(DestinationHandler destinationHandler) {

        return nest(path("/destination"), route(method(GET), destinationHandler::findAll));

    }

And this the handler function:

public Mono<ServerResponse> findAll(ServerRequest request) {
    Flux<Destination> destinations = destinationService.findAll();

    ResponseData responseData = buildResponseData(destinations);

    return ok().body(Mono.just(responseData), ResponseData.class);

}

private ResponseData buildResponseData(Flux<Destination> destinations) {
    ResponseData responseData = new ResponseData();
    responseData.setStatus(HttpStatus.OK.value());
    responseData.setCode(DESTINATION_LIST_SUCCESS_CODE);
    responseData.setMessage(DESTINATION_LIST_SUCCESS_MESSAGE);
    responseData.setData(destinations);
    return responseData;
}

And my ResponseData:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseData {
    private int status;
    private String code;
    private String message;
    private Object data;
}

Destination class:

//lombok annotations
public class Destination {
    private int id;
    private String local;
}

This result in this:

{ "status":200, "code":"destination_list_success", "message":"Retorno realizado com sucesso", "data":{ "scanAvailable":true, "prefetch":-1 } }

How can I return the correct destination list?


To do this, you need transform Flux<Destination> into a Mono<List<Destination>>, so make the following changes:

buildResponseData(Flux<Destination> destinations)

to:

buildResponseData(List<Destination> destinations)

and change return block to this:

return ok().body(destinations.collectList().map(d-> buildResponseData(d)), ResponseData.class);

23. WebFlux framework, To do this, you need transform Flux<Destination> into a Mono<List<Destination>​> , so make the following changes: buildResponseData(Flux<Destination>  Incoming requests are routed to handler functions with a RouterFunction<T> (i.e. Function<Request, Optional<HandlerFunction<T>>). A router function evaluates to a handler function if it matches; otherwise it returns an empty result. The RouterFunction has a similar purpose as a @RequestMapping annotation. However, there is an important distinction: with the annotation your route is limited to what can be expressed through the annotation values, and the processing of those is not trivial to


Try this

private ResponseData buildResponseData(Flux<Destination> destinations) 
{
    ResponseData responseData = new ResponseData();
    responseData.setStatus(HttpStatus.OK.value());
    responseData.setCode(DESTINATION_LIST_SUCCESS_CODE);
    responseData.setMessage(DESTINATION_LIST_SUCCESS_MESSAGE);
    responseData.setData(destinations.block());
    return responseData;
}

and also change ResponseData class to be

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseData {
    private int status;
    private String code;
    private String message;
    private List<...> data;
}

Web on Reactive Stack - Project Metadata API Guide, Spring Framework 5 includes a new spring-webflux module. The module listPeople is a handler function that returns all Person objects found in the repository as JSON. 2 Here is how we run a router function in Reactor Netty, for instance: 23.2 Spring WebFlux Module. Spring Framework 5 includes a new spring-webfluxmodule. The module contains supportfor reactive HTTP and WebSocket clients as well as for reactive server web applicationsincluding REST, HTML browser, and WebSocket style interactions. 23.2.1 Server Side.


You should flip the order. Following Carlos' answer:

return destinations.collectList().map(d-> ok().body(buildResponseData(d)));

Mastering Spring framework 5, Part 2: Spring WebFlux, Both Spring MVC and WebFlux controllers support reactive (Reactor and RxJava​) return types, and, as a result, it is not easy to tell them apart. Creating Multiple RouterFunctions in Spring WebFlux Spring WebFlux makes it incredibly easy to define router functions across different domains. This guide will walk you through the process and


Doing Stuff With Spring WebFlux, Build reactive web applications using Spring WebFlux annotations and For example, if we were constructing a list of objects and the map function returned RouterFunction<ServerResponse> route(BookHandler handler)  Look up a handler for the given request, returning an empty Mono if no specific one is found. This method is called by AbstractHandlerMapping.getHandler(org.springframework.web.server.ServerWebExchange).


Handling Errors in Spring WebFlux, This in-depth tutorial will get you started with Spring WebFlux, reactive programming, and building routes, handlers, and back-end requests. in a RequestPredicate along with a HandlerFunction and outputs a RouterFunction . as we were returning JSON representations of the Person object setting it to  RESTful web service with Spring WebFlux (new as of version 5) and then consumes that service with a WebClient (also new as of version 5). This guide shows the functional way of using Spring WebFlux. You can also use annotations with WebFlux .


Spring 5 WebFlux, 5. 6. @Bean. public RouterFunction<ServerResponse> routeRequest(Handler handler) {. return RouterFunctions.route(RequestPredicates. Spring WebFlux is a new functional web framework built using reactive principles. In this tutorial, we'll learn how to work with it in practice. We'll base this off of our existing guide to Spring 5 WebFlux .