-1

I'm trying to call an api with 2 call using webclient.

The first call return a token

The second call use the token and ask some data.

How to do it??

I've tried with call the first and use GetToken().block() but at runtime i have an error...

I've tried with :

GetToken().flatmap( x -> { GetDataRequest dataRequest = new GetDataRequest(x); 
return this.GetData(dataRequest);
}

this is the first call:

private Mono<GetTokenResponse> GetToken() {
return
weblicent.post().uri("GetToken").contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON_UTF8)
.syncBody(request)
.retrieve()
.bodyToMono(GetTokenResponse.class);
}

this.is the second call:

private Mono<GetDataResponse> GetData(GetDataRequest dataRequest) {
return
weblicent.post().uri("GetData")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON_UTF8)
.syncBody(dataRequest)
.retrieve()
.bodyToMono(GetDataResponse.class);
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
Mirko
  • 113
  • 2
  • 10
  • First what's the point of using reactive programming if you use block? In order to be reactive you have to chain the call and use the first call in your second call. What was your error message? – Xavier Bouclet Jun 10 '19 at 14:27

1 Answers1

-1

This is the last code:

public void getIndici() {
        try
        {
            ObjectMapper mapper = new ObjectMapper();


            this.GetToken().flatMap( tokenResponse -> {
                try
                {
                    String GetTokenResponse = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(tokenResponse);
                    log.info("DSGetTokenResponse:" + GetTokenResponse);

                    String token = tokenResponse.getTokenValue();
                    DSGetDataRequest dataRequest = new DSGetDataRequest(token, this.Richista_DS(), null);

                    String GetDataRequest = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(dataRequest);
                    log.info("DSGetDataRequest:" + GetDataRequest);

                    this.GetData(dataRequest).subscribe( dataResponse -> {
                        try {
                            String GetDataResponse = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(dataResponse);
                            log.info("DSGetDataResponse:" + GetDataResponse);
                        }
                        catch (Exception e) {
                            log.info("Sono in catch (Exception");
                            e.printStackTrace();

                        }

                    });



                } catch (Exception e) {
                    log.info("Sono in catch (Exception");
                    e.printStackTrace();

                }
            });

        } catch (Exception e) {
            log.info("Sono in catch (Exception");
            e.printStackTrace();
        }

    }

the error at compile time is :

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project XXXX-WebFlux: Compilation failure
[ERROR] /XXXXX-WebFlux/src/main/java/scaricodatiDSWSWebFlux/Services/Impl/ScaricaDatiService_Impl.java:[54,40] method flatMap in class reactor.core.publisher.Mono<T> cannot be applied to given types;
[ERROR]   required: java.util.function.Function<? super scaricodatiDSWSWebFlux.DTO.DSGetTokenResponse,? extends reactor.core.publisher.Mono<? extends R>>
[ERROR]   found: (tokenResp[...]; } }
[ERROR]   reason: cannot infer type-variable(s) R
[ERROR]     (argument mismatch; bad return type in lambda expression
[ERROR]       missing return value)
Mirko
  • 113
  • 2
  • 10