2

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.

Sonal Gupta
  • 31
  • 1
  • 5
  • are you meaning that you want to stream data to the server, full duplex? – Toerktumlare Aug 27 '20 at 15:14
  • actually i mean to say. I want to send data stream data to server. as well as like we want to fetch data in form of stream from server or database. like we get tweets. it is a form of flux response? – Sonal Gupta Aug 27 '20 at 17:49

1 Answers1

0

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:

HTTP versus Websockets

Toerktumlare
  • 12,548
  • 3
  • 35
  • 54
  • Okay .Thanks. I will try to get a clear understanding on this topic. I have edited my question. Can you please take a look once. – Sonal Gupta Aug 28 '20 at 05:42