我正在尝试使用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);
答案 0 :(得分:-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)