Mono switchIfEmpty() is always called

It’s because switchIfEmpty accepts Mono “by value”. Meaning that even before you subscribe to your mono, this alternative mono’s evaluation is already triggered. Imagine a method like this: Mono<String> asyncAlternative() { return Mono.fromFuture(CompletableFuture.supplyAsync(() -> { System.out.println(“Hi there”); return “Alternative”; })); } If you define your code like this: Mono<String> result = Mono.just(“Some payload”).switchIfEmpty(asyncAlternative()); It’ll always … Read more

what does Mono.defer() do?

It is a bit of an oversimplification but conceptually Reactor sources are either lazy or eager. More advanced ones, like an HTTP request, are expected to be lazily evaluated. On the other side the most simple ones like Mono.just or Flux.fromIterable are eager. By that, I mean that calling Mono.just(System.currentTimeMillis()) will immediately invoke the currentTimeMillis() … Read more

Using spring HATEOAS with spring webflux Functional Web Framework (reactor-netty)

By definition, you’re creating a custom route and Spring HATEOAS is an opinionated set of frameworks meant so you don’t have to lift a finger. What you are trying to do, and what Spring HATEOAS is doing are contradictory. So, you will have to manually create the payload if you want embedded hyperlinks. Although, this … Read more

How to customize SpringWebFlux WebClient JSON deserialization?

Here’s an example that customizes the ObjectMapper for JSON (de)serialization. Note that for streaming purposes, different encoders/decoders are being used but the principle remains the same for their configuration. ExchangeStrategies strategies = ExchangeStrategies .builder() .codecs(clientDefaultCodecsConfigurer -> { clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(new ObjectMapper(), MediaType.APPLICATION_JSON)); clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(new ObjectMapper(), MediaType.APPLICATION_JSON)); }).build(); WebClient webClient = WebClient.builder().exchangeStrategies(strategies).build();

Spring MVC (async) vs Spring WebFlux

The Servlet async model introduces an async boundary between the container threads (1 Servlet request/thread model) and the processing of the request in your application. Processing can happen on a different thread or wait. In the end, you have to dispatch back to a container thread and read/write in a blocking way (InputStream and OutputStream … Read more

how to log Spring 5 WebClient call

You can easily do it using ExchangeFilterFunction Just add the custom logRequest filter when you create your WebClient using WebClient.Builder. Here is the example of such filter and how to add it to the WebClient. @Slf4j @Component public class MyClient { private final WebClient webClient; // Create WebClient instance using builder. // If you use … Read more

DataBufferLimitException: Exceeded limit on max bytes to buffer webflux error

This worked for me: Create a @Bean in one of your configuration classes or the main SpringBootApplication class: @Bean public WebClient webClient() { final int size = 16 * 1024 * 1024; final ExchangeStrategies strategies = ExchangeStrategies.builder() .codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(size)) .build(); return WebClient.builder() .exchangeStrategies(strategies) .build(); } Next, go to your desired class where you want … Read more