Mono.Defer() vs Mono.create() vs Mono.just()?

Mono.just(value) is the most primitive – once you have a value you can wrap it into a Mono and subscribers down the line will get it. Mono.defer(monoSupplier) lets you provide the whole expression that supplies the resulting Mono instance. The evaluation of this expression is deferred until somebody subscribes. Inside of this expression you can … Read more

Spring WebFlux (Flux): how to publish dynamically

Publish “dynamically” using FluxProcessor and FluxSink One of the techniques to supply data manually to the Flux is using FluxProcessor#sink method as in the following example @SpringBootApplication @RestController public class DemoApplication { final FluxProcessor processor; final FluxSink sink; final AtomicLong counter; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } public DemoApplication() { this.processor = … Read more

Spring boot Webclient’s retrieve vs exchange

Adding to @JArgente’s answer. According to the official documentation of the retrieve() method: Perform the HTTP request and retrieve the response body. … This method is a shortcut to using exchange() and decoding the response body through ClientResponse. and the exchange() method Perform the HTTP request and return a ClientResponse with the response status and … Read more

How to get response body when testing the status code in WebFlux WebClient?

You could achieve like this by having a custom ExchangeFilterFunction and then hooking this up with WebClient.Builder before you build WebClient. public static ExchangeFilterFunction errorHandlingFilter() { return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> { if(clientResponse.statusCode()!=null && (clientResponse.statusCode().is5xxServerError() || clientResponse.statusCode().is4xxClientError()) ) { return clientResponse.bodyToMono(String.class) .flatMap(errorBody -> { return Mono.error(new CustomWebClientResponseException(errorBody,clientResponse.statusCode())); }); }else { return Mono.just(clientResponse); } }); } You can … Read more

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

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

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)