3 回答
TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊
每當(dāng)您使用 Spring-webflux 時(shí),您都必須記住一件事。即你不必打破你的鏈條。因?yàn)橛斜匾?,有人?yīng)該在你的鏈上調(diào)用訂閱。因?yàn)樗m用于 RXJava 規(guī)范。
如果你打破了鏈條,那么你必須打電話,block()這是不推薦的。
您必須按以下方式修改您的代碼。
讓我們假設(shè)您有一個(gè)處理程序正在調(diào)用您的collectEnvironmentData()方法,并且您的方法正在調(diào)用遠(yuǎn)程服務(wù)。
public Mono<ServerResponse> handelerMethod(ServerRequest request){
return collectEnvironmentData().flatMap(aVoid -> ServerResponse.ok().build());
}
你的方法應(yīng)該修改為
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);
}
按照上述方式改變你的實(shí)現(xiàn),希望它能奏效。
TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊
我將如何實(shí)現(xiàn)你的方法是:
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();
}
然后我會(huì)按如下方式使用它:
client.executeOnly(report).subscribe()
TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊
將 更改method return type為Mono<Void>進(jìn)行端到端流式傳輸。
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();
添加回答
舉報(bào)
