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

Right way to use Spring WebClient in multi-thread environment

Two key things here about WebClient: Its HTTP resources (connections, caches, etc) are managed by the underlying library, referenced by the ClientHttpConnector that you can configure on the WebClient WebClient is immutable With that in mind, you should try to reuse the same ClientHttpConnector across your application, because this will share the connection pool – … Read more

How to correctly read Flux and convert it to a single inputStream

This is really not as complicated as other answers imply. The only way to stream the data without buffering it all in memory is to use a pipe, as @jin-kwon suggested. However, it can be done very simply by using Spring’s BodyExtractors and DataBufferUtils utility classes. Example: private InputStream readAsInputStream(String url) throws IOException { PipedOutputStream … Read more

Java Spring WebFlux vs RxJava

Reactive programming is a programming paradigm, but I wouldn’t call it new. It’s actually been around for a while. Just like object-oriented programming, functional programming, or procedural programming, reactive programming is another programming paradigm. Paradigm is defined by Reactive Manifesto Reactive Streams, on the other hand, is a specification. For Java programmers, Reactive Streams is … Read more

Can I use SpringMvc and webflux together?

As explained in the Spring Boot reference documentation, Spring Boot will auto-configure a Spring MVC application if both MVC and WebFlux are available. There are several reasons for this: Spring MVC can’t run on Netty both infrastructure will compete for the same job (for example, serving static resources, the mappings, etc) mixing both runtime models … Read more

Threading model of Spring WebFlux and Reactor

After the question, the present documentation does provide some clues about the concurrency model and the threads one could expect (but I still think that clearer/better descriptions of what happens under-the-scene from a multi-threading perspective would be highly appreciated by Spring newcomers). It discusses the difference between Spring MVC and Spring WebFlux (1-thread-per-request model vs. … Read more

Backpressure mechanism in Spring Web-Flux

Backpressure in WebFlux In order to understand how Backpressure works in the current implementation of the WebFlux framework, we have to recap the transport layer used by default here. As we may remember, the normal communication between browser and server (server to server communication usually the same as well) is done through the TCP connection. … Read more

How to log request and response bodies in Spring WebFlux

This is more or less similar to the situation in Spring MVC. In Spring MVC, you can use a AbstractRequestLoggingFilter filter and ContentCachingRequestWrapper and/or ContentCachingResponseWrapper. Many tradeoffs here: if you’d like to access servlet request attributes, you need to actually read and parse the request body logging the request body means buffering the request body, … Read more

Spring Boot 2.0 disable default security

According to the new updates in Spring 2.0, if Spring Security is on the classpath, Spring Boot will add @EnableWebSecurity.So adding entries to the application.properties ain’t gonna work (i.e it is no longer customizable that way). For more information visit the official website Security changes in Spring Boot 2.0 Albeit not sure about your requirement … Read more

map vs flatMap in reactor

map is for synchronous, non-blocking, 1-to-1 transformations flatMap is for asynchronous (non-blocking) 1-to-N transformations The difference is visible in the method signature: map takes a Function<T, U> and returns a Flux<U> flatMap takes a Function<T, Publisher<V>> and returns a Flux<V> That’s the major hint: you can pass a Function<T, Publisher<V>> to a map, but it … Read more