How to send request body in spring-boot web client?
You’re not setting the “Content-Type” request header, so you need to append .contentType(MediaType.APPLICATION_JSON) to the request building part.
You’re not setting the “Content-Type” request header, so you need to append .contentType(MediaType.APPLICATION_JSON) to the request building part.
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
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
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();
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
2 semantics, 1 way to make them run in parallel The two options I present below both need some additional tuning to make A and B Mono run in parallel: namely, each Mono should use subscribeOn(Scheduler) to get out of the common thread from where they’re merged. If you only care about the completion of … Read more
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
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
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
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