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

How to set a timeout in Spring 5 WebFlux WebClient

To set the read and connect timeout I use the method below, because the SO_TIMEOUT option is not available for channels using NIO (and giving the warning Unknown channel option ‘SO_TIMEOUT’ for channel ‘[id: 0xa716fcb2]’) ReactorClientHttpConnector connector = new ReactorClientHttpConnector( options -> options.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000) .compression(true) .afterNettyContextInit(ctx -> { ctx.addHandlerLast(new ReadTimeoutHandler(5000, TimeUnit.MILLISECONDS)); })); return WebClient.builder() .clientConnector(connector) … Read more

How to post request with spring boot web-client for Form data for content type application/x-www-form-urlencoded

We can use BodyInserters.fromFormData for this purpose webClient client = WebClient.builder() .baseUrl(“SOME-BASE-URL”) .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE) .build(); return client.post() .uri(“SOME-URI) .body(BodyInserters.fromFormData(“username”, “SOME-USERNAME”) .with(“password”, “SONE-PASSWORD”)) .retrieve() .bodyToFlux(SomeClass.class) .onErrorMap(e -> new MyException(“messahe”,e)) .blockLast();

Reactive Spring does not support HttpServletRequest as parameter in REST endpoint?

You should never use the Servlet API in a Spring Reactive Web application. This is not supported and this is making your app container-dependent, whereas Spring Web Reactive can work with non-Servlet runtimes such as Netty. Instead you should use the HTTP API provided by Spring; here’s your code sample with a few changes: import … Read more

Spring WebClient: How to stream large byte[] to file?

With recent stable Spring WebFlux (5.2.4.RELEASE as of writing): final WebClient client = WebClient.create(“https://example.com”); final Flux<DataBuffer> dataBufferFlux = client.get() .accept(MediaType.TEXT_HTML) .retrieve() .bodyToFlux(DataBuffer.class); // the magic happens here final Path path = FileSystems.getDefault().getPath(“target/example.html”); DataBufferUtils .write(dataBufferFlux, path, CREATE_NEW) .block(); // only block here if the rest of your code is synchronous For me the non-obvious part was … Read more

Spring WebFlux differences when Netty vs. Tomcat is used under the hood

Currently there are 2 basic concepts to handle parallel access to a web-server with various advantages and disadvantages: Blocking Non-Blocking Blocking Web-Servers The first concept of blocking, multi-threaded server has a finite set amount of threads in a pool. Every request will get assigned to specific thread and this thread will be assigned until the … 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

Spring Webflux + JPA: Reactive Repositories are not supported by JPA

If you want all the benefits of reactive, async / non-blocking, you’ll need to make the whole stack async / non-blocking. JDBC is indeed inherently a blocking API, so you can’t build a fully reactive / non-blocking app if you need to access the database through JDBC. But you still you need relational database then … Read more