From 5b1e5979782cbf0d6b468adcb01ef370df43a09a Mon Sep 17 00:00:00 2001 From: Gerardo Roza Date: Thu, 21 Jan 2021 11:47:41 -0300 Subject: [PATCH 01/10] Updated deprecated BodyInserters.fromObject method, in favor of BodyInserters.fromValue --- .../com/baeldung/web/reactive/client/WebClientController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java index a719259328..09134701e9 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java +++ b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java @@ -38,7 +38,7 @@ public class WebClientController { // request header specification WebClient.RequestHeadersSpec requestSpec1 = uri1.body(BodyInserters.fromPublisher(Mono.just("data"), String.class)); - WebClient.RequestHeadersSpec requestSpec2 = uri2.body(BodyInserters.fromObject("data")); + WebClient.RequestHeadersSpec requestSpec2 = uri2.body(BodyInserters.fromValue("data")); // inserters BodyInserter, ReactiveHttpOutputMessage> inserter1 = BodyInserters @@ -49,7 +49,7 @@ public class WebClientController { map.add("key2", "value2"); // BodyInserter, ClientHttpRequest> inserter2 = BodyInserters.fromMultipartData(map); - BodyInserter inserter3 = BodyInserters.fromObject("body"); + BodyInserter inserter3 = BodyInserters.fromValue("body"); // responses WebClient.ResponseSpec response1 = uri1.body(inserter3) From 14506a542df37ff132f307a35eeb91dfeba7dd66 Mon Sep 17 00:00:00 2001 From: Gerardo Roza Date: Thu, 21 Jan 2021 11:50:55 -0300 Subject: [PATCH 02/10] removed unused reactor-spring version property --- spring-5-reactive/pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-5-reactive/pom.xml b/spring-5-reactive/pom.xml index 3b7383f726..60c8b90e16 100644 --- a/spring-5-reactive/pom.xml +++ b/spring-5-reactive/pom.xml @@ -149,7 +149,6 @@ - 1.0.1.RELEASE 1.1.3 1.0 1.0 From 52d458c05f5c1dd2eec3bb6aca1b2b0f9d362de7 Mon Sep 17 00:00:00 2001 From: Gerardo Roza Date: Thu, 21 Jan 2021 12:43:26 -0300 Subject: [PATCH 03/10] formatted WebClientController class --- .../reactive/client/WebClientController.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java index 09134701e9..7c0dc1cff7 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java +++ b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java @@ -1,8 +1,17 @@ package com.baeldung.web.reactive.client; +import java.net.URI; +import java.nio.charset.Charset; +import java.time.ZonedDateTime; +import java.util.Collections; + import org.reactivestreams.Publisher; import org.reactivestreams.Subscriber; -import org.springframework.http.*; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ReactiveHttpOutputMessage; import org.springframework.util.LinkedMultiValueMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseStatus; @@ -10,12 +19,8 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.reactive.function.BodyInserter; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.WebClient; -import reactor.core.publisher.Mono; -import java.net.URI; -import java.nio.charset.Charset; -import java.time.ZonedDateTime; -import java.util.Collections; +import reactor.core.publisher.Mono; @RestController public class WebClientController { @@ -41,8 +46,7 @@ public class WebClientController { WebClient.RequestHeadersSpec requestSpec2 = uri2.body(BodyInserters.fromValue("data")); // inserters - BodyInserter, ReactiveHttpOutputMessage> inserter1 = BodyInserters - .fromPublisher(Subscriber::onComplete, String.class); + BodyInserter, ReactiveHttpOutputMessage> inserter1 = BodyInserters.fromPublisher(Subscriber::onComplete, String.class); LinkedMultiValueMap map = new LinkedMultiValueMap<>(); map.add("key1", "value1"); From dd60782928c438d33d05c07889eba964c3f62318 Mon Sep 17 00:00:00 2001 From: Gerardo Roza Date: Thu, 21 Jan 2021 12:44:06 -0300 Subject: [PATCH 04/10] Added timeout WebClient configuration, as in article --- .../reactive/client/WebClientController.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java index 7c0dc1cff7..3132a4173d 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java +++ b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java @@ -4,6 +4,7 @@ import java.net.URI; import java.nio.charset.Charset; import java.time.ZonedDateTime; import java.util.Collections; +import java.util.concurrent.TimeUnit; import org.reactivestreams.Publisher; import org.reactivestreams.Subscriber; @@ -12,6 +13,7 @@ import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ReactiveHttpOutputMessage; +import org.springframework.http.client.reactive.ReactorClientHttpConnector; import org.springframework.util.LinkedMultiValueMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseStatus; @@ -20,7 +22,12 @@ import org.springframework.web.reactive.function.BodyInserter; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.WebClient; +import io.netty.channel.ChannelOption; +import io.netty.handler.timeout.ReadTimeoutHandler; +import io.netty.handler.timeout.WriteTimeoutHandler; import reactor.core.publisher.Mono; +import reactor.netty.http.client.HttpClient; +import reactor.netty.tcp.TcpClient; @RestController public class WebClientController { @@ -75,6 +82,19 @@ public class WebClientController { return WebClient.create("http://localhost:8081"); } + private WebClient createWebClientConfiguringTimeout() { + TcpClient tcpClient = TcpClient.create() + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) + .doOnConnected(connection -> { + connection.addHandlerLast(new ReadTimeoutHandler(5000, TimeUnit.MILLISECONDS)); + connection.addHandlerLast(new WriteTimeoutHandler(5000, TimeUnit.MILLISECONDS)); + }); + + return WebClient.builder() + .clientConnector(new ReactorClientHttpConnector(HttpClient.from(tcpClient))) + .build(); + } + private WebClient createWebClientWithServerURLAndDefaultValues() { return WebClient.builder() .baseUrl("http://localhost:8081") From 1f7e0f06b6d8286f8a053ad38a385ef41bd55bec Mon Sep 17 00:00:00 2001 From: Gerardo Roza Date: Thu, 21 Jan 2021 14:25:30 -0300 Subject: [PATCH 05/10] updated timeout logic due to deprecation --- .../web/reactive/client/WebClientController.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java index 3132a4173d..66f787eda3 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java +++ b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java @@ -27,7 +27,6 @@ import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.handler.timeout.WriteTimeoutHandler; import reactor.core.publisher.Mono; import reactor.netty.http.client.HttpClient; -import reactor.netty.tcp.TcpClient; @RestController public class WebClientController { @@ -83,15 +82,13 @@ public class WebClientController { } private WebClient createWebClientConfiguringTimeout() { - TcpClient tcpClient = TcpClient.create() + HttpClient httpClient = HttpClient.create() .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) - .doOnConnected(connection -> { - connection.addHandlerLast(new ReadTimeoutHandler(5000, TimeUnit.MILLISECONDS)); - connection.addHandlerLast(new WriteTimeoutHandler(5000, TimeUnit.MILLISECONDS)); - }); + .doOnConnected(conn -> conn.addHandlerLast(new ReadTimeoutHandler(5000, TimeUnit.MILLISECONDS)) + .addHandlerLast(new WriteTimeoutHandler(5000, TimeUnit.MILLISECONDS))); return WebClient.builder() - .clientConnector(new ReactorClientHttpConnector(HttpClient.from(tcpClient))) + .clientConnector(new ReactorClientHttpConnector(httpClient)) .build(); } From e6ab521b6127a9949515da3ad47aac66e8963cf9 Mon Sep 17 00:00:00 2001 From: Gerardo Roza Date: Thu, 21 Jan 2021 14:57:06 -0300 Subject: [PATCH 06/10] added LinkedMultiValueMap usage for inserter declaration, as in article --- .../com/baeldung/web/reactive/client/WebClientController.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java index 66f787eda3..34de13bc28 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java +++ b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java @@ -13,8 +13,10 @@ import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ReactiveHttpOutputMessage; +import org.springframework.http.client.reactive.ClientHttpRequest; import org.springframework.http.client.reactive.ReactorClientHttpConnector; import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; @@ -58,7 +60,7 @@ public class WebClientController { map.add("key1", "value1"); map.add("key2", "value2"); - // BodyInserter, ClientHttpRequest> inserter2 = BodyInserters.fromMultipartData(map); + BodyInserter, ClientHttpRequest> inserter2 = BodyInserters.fromMultipartData(map); BodyInserter inserter3 = BodyInserters.fromValue("body"); // responses From 10318d1145c2d2a87ec3e5d718aa8122c2609a9a Mon Sep 17 00:00:00 2001 From: Gerardo Roza Date: Fri, 22 Jan 2021 10:29:15 -0300 Subject: [PATCH 07/10] updated inserter3 as in article --- .../com/baeldung/web/reactive/client/WebClientController.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java index 34de13bc28..70919d8a2e 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java +++ b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java @@ -61,7 +61,8 @@ public class WebClientController { map.add("key2", "value2"); BodyInserter, ClientHttpRequest> inserter2 = BodyInserters.fromMultipartData(map); - BodyInserter inserter3 = BodyInserters.fromValue("body"); + BodyInserter inserter3 = BodyInserters.fromValue(new Object()); + BodyInserter inserter4 = BodyInserters.fromValue("body"); // responses WebClient.ResponseSpec response1 = uri1.body(inserter3) From 814369e38cf68f9e3ff428c92bca3f044520bcf2 Mon Sep 17 00:00:00 2001 From: Gerardo Roza Date: Fri, 22 Jan 2021 10:49:47 -0300 Subject: [PATCH 08/10] added exchange and retrieve examples as in teh article, updated due to deprecation --- .../baeldung/web/reactive/client/WebClientController.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java index 70919d8a2e..de4f20a13f 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java +++ b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java @@ -72,8 +72,12 @@ public class WebClientController { .ifNoneMatch("*") .ifModifiedSince(ZonedDateTime.now()) .retrieve(); - WebClient.ResponseSpec response2 = requestSpec2.retrieve(); - + String response2 = uri1.exchangeToMono(response -> response.bodyToMono(String.class)) + .block(); + String response3 = uri2.retrieve() + .bodyToMono(String.class) + .block(); + WebClient.ResponseSpec response4 = requestSpec2.retrieve(); } private WebClient createWebClient() { From 1b78d4be310efbb6dbaacac028b2e14305738e60 Mon Sep 17 00:00:00 2001 From: Gerardo Roza Date: Fri, 22 Jan 2021 12:33:54 -0300 Subject: [PATCH 09/10] fixed and improved WebClient integration test --- .../reactive/client/WebClientApplication.java | 13 +++++ .../reactive/client/WebClientController.java | 7 ++- .../client/WebTestClientIntegrationTest.java | 54 ++++++++++++++++--- 3 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientApplication.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientApplication.java b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientApplication.java new file mode 100644 index 0000000000..f104ad30f1 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.web.reactive.client; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class WebClientApplication{ + + public static void main(String[] args) { + SpringApplication.run(WebClientApplication.class, args); + } + +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java index de4f20a13f..2d42e848b2 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java +++ b/spring-5-reactive/src/main/java/com/baeldung/web/reactive/client/WebClientController.java @@ -4,6 +4,8 @@ import java.net.URI; import java.nio.charset.Charset; import java.time.ZonedDateTime; import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.TimeUnit; import org.reactivestreams.Publisher; @@ -35,7 +37,10 @@ public class WebClientController { @ResponseStatus(HttpStatus.OK) @GetMapping("/resource") - public void getResource() { + public Map getResource() { + Map response = new HashMap<>(); + response.put("field", "value"); + return response; } public void demonstrateWebClient() { diff --git a/spring-5-reactive/src/test/java/com/baeldung/web/client/WebTestClientIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/web/client/WebTestClientIntegrationTest.java index 2e37f2ffbd..07a4c81c91 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/web/client/WebTestClientIntegrationTest.java +++ b/spring-5-reactive/src/test/java/com/baeldung/web/client/WebTestClientIntegrationTest.java @@ -1,11 +1,11 @@ package com.baeldung.web.client; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.context.ApplicationContext; import org.springframework.security.test.context.support.WithMockUser; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.web.reactive.function.server.RequestPredicates; import org.springframework.web.reactive.function.server.RouterFunction; @@ -13,18 +13,23 @@ import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.server.WebHandler; -import com.baeldung.reactive.Spring5ReactiveApplication; +import com.baeldung.web.reactive.client.WebClientApplication; +import com.baeldung.web.reactive.client.WebClientController; import reactor.core.publisher.Mono; -@RunWith(SpringRunner.class) -@SpringBootTest(classes = Spring5ReactiveApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@WithMockUser +@SpringBootTest(classes = WebClientApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class WebTestClientIntegrationTest { @LocalServerPort private int port; + @Autowired + private ApplicationContext context; + + @Autowired + private WebClientController controller; + private final RouterFunction ROUTER_FUNCTION = RouterFunctions.route(RequestPredicates.GET("/resource"), request -> ServerResponse.ok() .build()); private final WebHandler WEB_HANDLER = exchange -> Mono.empty(); @@ -49,6 +54,7 @@ public class WebTestClientIntegrationTest { } @Test + @WithMockUser public void testWebTestClientWithServerURL() { WebTestClient.bindToServer() .baseUrl("http://localhost:" + port) @@ -58,7 +64,39 @@ public class WebTestClientIntegrationTest { .exchange() .expectStatus() .isOk() - .expectBody(); + .expectBody() + .jsonPath("field") + .isEqualTo("value"); + ; + } + + @Test + @WithMockUser + public void testWebTestClientWithApplicationContext() { + WebTestClient.bindToApplicationContext(context) + .build() + .get() + .uri("/resource") + .exchange() + .expectStatus() + .isOk() + .expectBody() + .jsonPath("field") + .isEqualTo("value"); + } + + @Test + public void testWebTestClientWithController() { + WebTestClient.bindToController(controller) + .build() + .get() + .uri("/resource") + .exchange() + .expectStatus() + .isOk() + .expectBody() + .jsonPath("field") + .isEqualTo("value"); } } From 30f0c13ef637377122af55df319774d31c66ce53 Mon Sep 17 00:00:00 2001 From: Gerardo Roza Date: Fri, 22 Jan 2021 12:48:34 -0300 Subject: [PATCH 10/10] added context test for new WebClientApplication --- .../com/baeldung/web/client/SpringContextTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 spring-5-reactive/src/test/java/com/baeldung/web/client/SpringContextTest.java diff --git a/spring-5-reactive/src/test/java/com/baeldung/web/client/SpringContextTest.java b/spring-5-reactive/src/test/java/com/baeldung/web/client/SpringContextTest.java new file mode 100644 index 0000000000..8d2ca41451 --- /dev/null +++ b/spring-5-reactive/src/test/java/com/baeldung/web/client/SpringContextTest.java @@ -0,0 +1,14 @@ +package com.baeldung.web.client; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import com.baeldung.web.reactive.client.WebClientApplication; + +@SpringBootTest(classes = WebClientApplication.class) +public class SpringContextTest { + + @Test + public void whenSpringContextIsBootstrapped_thenNoExceptions() { + } +}