Enable CORS in Spring 5 Webflux?

I had success with this custom filter: import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.web.cors.reactive.CorsUtils; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebFilter; import org.springframework.web.server.WebFilterChain; import reactor.core.publisher.Mono; @Configuration public class CorsConfiguration { private static final String ALLOWED_HEADERS = “x-requested-with, authorization, Content-Type, Authorization, credential, X-XSRF-TOKEN”; private static final String ALLOWED_METHODS = “GET, … Read more

Don’t spring-boot-starter-web and spring-boot-starter-webflux work together?

As explained in the Spring Boot reference documentation section about the web environment, adding both web and webflux starters will configure a Spring MVC web application. This is behaving like that, because many existing Spring Boot web applications (using MVC) will depend on the webflux starter to use the WebClient. Spring MVC partially support reactive … Read more

Spring 5 WebClient using ssl

See example of use insecure TrustManagerFactory that trusts all X.509 certificates (including self-signed) without any verification. The important note from documentation: Never use this TrustManagerFactory in production. It is purely for testing purposes, and thus it is very insecure. @Bean public WebClient createWebClient() throws SSLException { SslContext sslContext = SslContextBuilder .forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE) .build(); ClientHttpConnector httpConnector … 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

Fire and forget with reactor

1, If your fire-and-forget is already async returning Mono/Flux public Flux<Data> search(SearchRequest request) { return searchService.search(request) .collectList() .doOnNext(data -> doThisAsync(data).subscribe()) // add error logging here or inside doThisAsync .flatMapMany(Flux::fromIterable); } public Mono<Void> doThisAsync(List<Data> data) { //do some async/non-blocking processing here like calling WebClient } 2, If your fire-and-forget does blocking I/O public Flux<Data> search(SearchRequest request) … Read more