3 回答

TA貢獻1860條經驗 獲得超8個贊
每當您使用 Spring-webflux 時,您都必須記住一件事。即你不必打破你的鏈條。因為有必要,有人應該在你的鏈上調用訂閱。因為它適用于 RXJava 規范。
如果你打破了鏈條,那么你必須打電話,block()這是不推薦的。
您必須按以下方式修改您的代碼。
讓我們假設您有一個處理程序正在調用您的collectEnvironmentData()方法,并且您的方法正在調用遠程服務。
public Mono<ServerResponse> handelerMethod(ServerRequest request){
return collectEnvironmentData().flatMap(aVoid -> ServerResponse.ok().build());
}
你的方法應該修改為
public Mono<Void> collectEnvironmentData() throws JAXBException {
ReportRequest report = new ReportRequest();
report.setVersion("1.0");
RestClient client = null;
try {
client = RestClientBuilder.builder()
.gatewayUrl(URL2)
// .token(contract.getTerminal_token())
// .usernamePassword("user", "password")
// .truststore(new File("server.pem"))
// .sslCertificate(new File("client.pem"), new File("clientKey.p8"),
//"secret").build();
} catch (SSLException e) {
e.printStackTrace();
}
return client.executeOnly(report);
}
按照上述方式改變你的實現,希望它能奏效。

TA貢獻1825條經驗 獲得超6個贊
我將如何實現你的方法是:
public Mono<Void> executeOnly(ReportRequest transaction) {
Mono<ReportRequest> transactionMono = Mono.just(transaction);
return client.post().uri(gatewayUrl)
.accept(MediaType.APPLICATION_XML)
.contentType(MediaType.APPLICATION_XML)
.body(transaction, ReportRequest.class)
.exchange()
.then();
}
然后我會按如下方式使用它:
client.executeOnly(report).subscribe()

TA貢獻1757條經驗 獲得超7個贊
將 更改method return type為Mono<Void>進行端到端流式傳輸。
public void collectEnvironmentData() throws JAXBException {
ReportRequest report = new ReportRequest();
report.setVersion("1.0");
RestClient client = null;
try {
client = RestClientBuilder.builder()
.gatewayUrl(URL2)
.build();
} catch (SSLException e) {
e.printStackTrace();
}
return client.executeOnly(report);
}
或者您也可以訂閱Mono
client.executeOnly(report).subscribe();
添加回答
舉報