I am new to Spring. I was trying to make a sample application for spring webflux in functional way. Why can't our handler function pass Flux. is there any way to make router function accept it as it is said that router function accept a subtype of serverResponse.
Show Handler code
public Mono<ServerResponse> getShowList(ServerRequest request){
Flux<Show> showList = showRepository.findAll();
Flux<ShowVo> showVoList= showList.map(s -> {
return new ShowVo(s.getId(), s.getTitle());
});
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(showVoList, ShowVo.class); }
Here i am passing the Mono <ServerResponse> but I want to it as Flux <ServerResponse> to the Router function
Router function code
@Bean
public RouterFunction<ServerResponse> routeShow(ShowHandler showHandler){
return RouterFunctions.route(RequestPredicates.GET("/shows").and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), showHandler::getShowList)
}
}
So is there any way to do it, I have gone through different articles. All I can find is Mono but if I use annotation based webflux I can pass flux.
Why the webhandler doesn't accept a stream to the server, and you can only return a stream is because of the HTTP protocol specification.
If you wish to both stream data to the server, and stream data to the client (full duplex) you need to use websockets with webflux.
you can read all about it in the webflux documentation:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.