Spring Boot - сделать новый вызов WebClient с результатом другого вызова - PullRequest
0 голосов
/ 10 июня 2019

Я пытаюсь вызвать API с 2 вызова с помощью веб-клиента.

Первый звонок вернет токен

Во втором вызове используйте токен и запросите некоторые данные.

Как это сделать ??

Я пытался с вызовом первым и использовать GetToken().block(), но во время выполнения у меня ошибка ...

Я пробовал с:

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

это первый звонок:

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

это второй звонок:

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);

1 Ответ

0 голосов
/ 10 июня 2019

Это последний код:

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();
        }

    }

ошибка во время компиляции:

[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)
...