【问题标题】:Spring Boot - Do a new WebClient call with result of another callSpring Boot - 使用另一个调用的结果进行新的 WebClient 调用
【发布时间】:2019-10-24 22:59:33
【问题描述】:

我正在尝试使用 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);
}

this.is 第二次调用:

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

【问题讨论】:

  • 首先,如果您使用块,那么使用响应式编程有什么意义?为了做出反应,您必须链接呼叫并在第二个呼叫中使用第一个呼叫。你的错误信息是什么?

标签: spring-boot spring-webclient


【解决方案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)

【讨论】:

  • 您的平面图中第 54 行的编译错误,您没有从中返回。
猜你喜欢
  • 2019-10-21
  • 2021-09-07
  • 2018-09-16
  • 2022-01-08
  • 2021-12-23
  • 2018-10-16
  • 2020-03-22
  • 2019-02-14
相关资源
最近更新 更多