猿问

如何组合两个异步 webclient 调用,假设一个调用遇到异常

我正在使用 spring boot 2.x 并使用 webclient 进行两个异步调用,我在一个调用中得到正确的响应,而另一个调用遇到一些异常。我想使用 zip 方法将两个响应压缩在一起,但是在使用带 zip 的块时,它会抛出异常和控制流以捕获块。我希望两个响应都被压缩,其中一个或两个都存在异常。请指导我如何做到这一点。


Mono<BookResponse>  bookResponseMono =webClient.get()

           .uri("/getBooking/" + bookingId).headers(headers->headers.addAll(args) 

           .retrieve()

           .bodyToMono(BookResponse.class);// with proper responce

Mono<Address>  addressResponseMono =webClient.get()

           .uri("/getAddress/" + bookingId)

           .headers(headers->headers.addAll(args))

           .retrieve()

           .bodyToMono(Address.class);// encounter readtimeout exception


Tuple2<BookResponse, Address> resp = bookResponseMono.zipWith(addressResponseMono).block();// throws exception but 

我想压缩两个响应以及异常。


千万里不及你
浏览 105回答 1
1回答

当年话下

onErrorResume为我解决上述问题。bookResponseMono = webClient.get()&nbsp; &nbsp; .uri("/getBooking/" + bookingId)&nbsp; &nbsp; .headers(headers->headers.addAll(args))&nbsp; &nbsp; .retrieve()&nbsp; &nbsp; .bodyToMono(String.class)&nbsp; &nbsp; .onErrorResume(err -> {&nbsp; &nbsp; &nbsp; &nbsp; BookResponse bookResponse = new BookResponse();&nbsp; &nbsp; &nbsp; &nbsp; bookResponse.setError(setError(err));&nbsp; &nbsp; &nbsp; &nbsp; return Mono.just(setError(err));&nbsp; &nbsp; });addressResponseMono = webClient.get()&nbsp; &nbsp; .uri("/getAddress/" + bookingId)&nbsp; &nbsp; .headers(headers -> headers.addAll(args))&nbsp; &nbsp; .retrieve()&nbsp; &nbsp; .bodyToMono(String.class)&nbsp; &nbsp; .onErrorResume(err -> {&nbsp; &nbsp; &nbsp; &nbsp; Address address = new Address();&nbsp; &nbsp; &nbsp; &nbsp; address.setError(setError(err));&nbsp; &nbsp; &nbsp; &nbsp; return Mono.just(setError(err));&nbsp; &nbsp; });最后拉上拉链bookAndAddressResponse = bookResponseMono&nbsp; &nbsp; .zipWith(addressResponseMono, BookAndAddressResponse::new)&nbsp; &nbsp; .block();
随时随地看视频慕课网APP

相关分类

Java
我要回答