How to extract response header & status code from Spring 5 WebClient ClientResponse

You can use the exchange function of webclient e.g. Mono<String> reponse = webclient.get() .uri(“https://stackoverflow.com”) .exchange() .doOnSuccess(clientResponse -> System.out.println(“clientResponse.headers() = ” + clientResponse.headers())) .doOnSuccess(clientResponse -> System.out.println(“clientResponse.statusCode() = ” + clientResponse.statusCode())) .flatMap(clientResponse -> clientResponse.bodyToMono(String.class)); then you can convert bodyToMono etc

Spring 5 WebClient using ssl

Looks like Spring 5.1.1 (Spring boot 2.1.0) removed HttpClientOptions from ReactorClientHttpConnector, so you can not configure options while creating instance of ReactorClientHttpConnector One option that works now is: val sslContext = SslContextBuilder .forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE) .build() val httpClient = HttpClient.create().secure { t -> t.sslContext(sslContext) } val webClient = WebClient.builder().clientConnector(ReactorClientHttpConnector(httpClient)).build() Basically while creating the HttpClient, we are … Read more

Deserialize a json array to objects using Jackson and WebClient

Regarding your updated answer to your question, using bodyToFlux is unnecessarily inefficient and semantically doesn’t make much sense either as you don’t really want a stream of orders. What you want is simply to be able to parse the response as a list. bodyToMono(List<AccountOrder>.class) won’t work due to type erasure. You need to be able … Read more

Timeout on blocking read for 5000 MILLISECONDS in Spring Webflux

I was seeing similar issue and Exception when running Integration tests some of them aggregates responses from multiple other services which has database access and stuff. So we were seeing this issue intermittently when running Integration tests. We are using Spring Boot 2.0.0.RC1 and Junit 5 with Gradle. I did this to resolve the issue. … Read more

What is then, thenEmpty, thenMany and flatMapMany in spring webflux?

flatMap vs flatMapMany In functional programming, flatMap returns the same type than the type that bear the method, so for Mono<T>, flatMap returns a Mono. Which means that only one element can be emitted by the inner Publisher (or that it is truncated). We enforced that by having Mono#flatMap take a Function<T, Mono<R>>. As a … Read more

Difference between @Controller and RouterFunction in Spring 5 WebFlux

Programming Paradigm: Imperative vs Functional In the case with the @Controller or @RestController annotations, we agree with the annotation-based model where we use annotations for mappings (and not only) and as a result side effects (that is not allowed in the functional world) to make our API works. Such side effects could be @Valid annotation … Read more

block()/blockFirst()/blockLast() are blocking error when calling bodyToMono AFTER exchange()

First, a few things that will help you understand the code snippet solving this use case. You should never call a blocking method within a method that returns a reactive type; you will block one of the few threads of your application and it is very bad for the application Anyway as of Reactor 3.2, … Read more

Spring Webflux : Webclient : Get body on error

I prefer to use the methods provided by the ClientResponse to handle http errors and throw exceptions: WebClient.create() .post() .uri( url ) .body( bodyObject == null ? null : BodyInserters.fromValue( bodyObject ) ) .accept( MediaType.APPLICATION_JSON ) .headers( headers ) .exchange() .flatMap( clientResponse -> { //Error handling if ( clientResponse.statusCode().isError() ) { // or clientResponse.statusCode().value() >= … Read more

Spring Webflux : Webclient : Get body on error

I prefer to use the methods provided by the ClientResponse to handle http errors and throw exceptions: WebClient.create() .post() .uri( url ) .body( bodyObject == null ? null : BodyInserters.fromValue( bodyObject ) ) .accept( MediaType.APPLICATION_JSON ) .headers( headers ) .exchange() .flatMap( clientResponse -> { //Error handling if ( clientResponse.statusCode().isError() ) { // or clientResponse.statusCode().value() >= … Read more