1

I am working on spring reactive and need to call multiple calls sequentially to other REST API using webclient. The issue is I am able to call multiple calls to other Rest API but response am not able to read without subscribe or block. I can't use subscribe or block due to non reactive programming. Is there any way, i can merge while reading the response and send it as flux. Below is the piece of code where I am stuck.

public Mono<DownloadDataLog> getDownload(Token dto, Mono<DataLogRequest> request) {
    Mono<GraphQlCustomerResponse> profileResponse = customerProfileHandler.getMyUsageHomeMethods(dto, null);
    DownloadDataLog responseObj = new DownloadDataLog();
    ArrayList<Mono<List<dataUsageLogs>>> al = new ArrayList<>();
    return Mono.zip(profileResponse, request).flatMap(tuple2 -> {
        Flux<List<Mono<DataLogGqlRequest>>> userequest = prepareUserRequest(getListOfMdns(tuple2.getT1()),
                tuple2.getT2());              
        Flux.from(userequest).flatMap(req -> {
            for (Mono<DataLogGqlRequest> logReq : req) {
                al.add(service.execute(logReq, dto));
            }
            responseObj.setAl(al);
            return Mono.empty();
        }).subscribe();
          return Mono.just(responseObj);

    });

}

private Mono<DataLogGqlRequest> prepareInnerRequest(Mono<DataLogGqlRequest> itemRequest, int v1,int v2){
    return itemRequest.flatMap(req -> {
        DataLogGqlRequest userRequest = new DataLogGqlRequest();
        userRequest.setBillDate(req.getBillDate()); 
        userRequest.setMdnNumber(req.getMdnNumber());
        userRequest.setCposition(v1+"");    
        userRequest.setPposition(v2+"");
        return Mono.just(userRequest);
    });
    
    
}
  • Hello Rahul and welcome to StackOverflow. I have some trouble understanding exactly what you are asking. Once you use a `Future` (I think a reactive subscription can be abstracted as a `Future`, probably), you do not know when it comes back; hence, expecting synchronous behaviour from an asynchronous process will result in extreme frustration, IMHO. I think you may want to `flatMap` everything. The documentation of your framework may be a good starting point, to see how you can achieve this. I think your framework use lazy fetching, so `.subscribe(...)` is probably a must. – Sumi Straessle Apr 16 '22 at 20:16
  • It's very hard to understand the example but you could heck similar question here https://stackoverflow.com/questions/71484735/how-to-use-spring-webclient-to-make-multiple-calls-simultaneously-and-get-respon/71485024#71485024 – Alex Apr 16 '22 at 20:53
  • Hi @Alex i need to make a call to the same service with different request and then collect all the response and merge it as single response. – Rahul1857 Rahul Apr 18 '22 at 17:42
  • @Alex i am getting null response from the service and the list is empty.Could you please help me . – Rahul1857 Rahul Apr 18 '22 at 17:48

0 Answers0