-1

我正在尝试使用 webclient 调用 2 个调用的 api。

第一次调用返回一个令牌

第二次调用使用令牌并询问一些数据。

怎么做??

我试过调用第一个并使用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);
4

1 回答 1

-1

这是最后的代码:

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)
于 2019-06-10T14:41:53.447 回答