Skip to content

Commit 806c17b

Browse files
andrewtobilkopivovarit
authored andcommitted
BAEL-878 [2] (eugenp#2194)
* code snippets for the `Java 9 Stream API improvements` article * code snippets for the `Java 9 Stream API improvements` article [2 attempt] * removed the first attempt * the Spring 5 WebClient * delted stream features test * HttpMediaTypeNotAcceptableExceptionExampleController [0] * reactive web client service was removed * new WebClient * new WebClient [2]
1 parent ad5696a commit 806c17b

3 files changed

Lines changed: 149 additions & 0 deletions

File tree

‎spring-5/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
<groupId>org.springframework.boot</groupId>
4040
<artifactId>spring-boot-starter-webflux</artifactId>
4141
</dependency>
42+
<dependency>
43+
<groupId>org.projectreactor</groupId>
44+
<artifactId>reactor-spring</artifactId>
45+
<version>1.0.1.RELEASE</version>
46+
</dependency>
4247

4348
<!-- utils -->
4449
<dependency>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.baeldung.web.reactive.client;
2+
3+
import org.reactivestreams.Publisher;
4+
import org.reactivestreams.Subscriber;
5+
import org.springframework.http.*;
6+
import org.springframework.http.client.reactive.ClientHttpRequest;
7+
import org.springframework.util.LinkedMultiValueMap;
8+
import org.springframework.util.MultiValueMap;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.ResponseStatus;
11+
import org.springframework.web.bind.annotation.RestController;
12+
import org.springframework.web.reactive.function.BodyInserter;
13+
import org.springframework.web.reactive.function.BodyInserters;
14+
import org.springframework.web.reactive.function.client.WebClient;
15+
import reactor.core.publisher.Mono;
16+
17+
import java.net.URI;
18+
import java.nio.charset.Charset;
19+
import java.time.ZonedDateTime;
20+
import java.util.Collections;
21+
22+
@RestController
23+
public class WebClientController {
24+
25+
@ResponseStatus(HttpStatus.OK)
26+
@GetMapping("/resource")
27+
public void getResource() {
28+
}
29+
30+
public void demonstrateWebClient() {
31+
// request
32+
WebClient.UriSpec<WebClient.RequestBodySpec> request1 = createWebClientWithServerURLAndDefaultValues().method(HttpMethod.POST);
33+
WebClient.UriSpec<WebClient.RequestBodySpec> request2 = createWebClientWithServerURLAndDefaultValues().post();
34+
35+
// request body specifications
36+
WebClient.RequestBodySpec uri1 = createWebClientWithServerURLAndDefaultValues().method(HttpMethod.POST).uri("/resource");
37+
WebClient.RequestBodySpec uri2 = createWebClientWithServerURLAndDefaultValues().post().uri(URI.create("/resource"));
38+
39+
// request header specification
40+
WebClient.RequestHeadersSpec<?> requestSpec1 = uri1.body(BodyInserters.fromPublisher(Mono.just("data"), String.class));
41+
WebClient.RequestHeadersSpec<?> requestSpec2 = uri2.body(BodyInserters.fromObject("data"));
42+
43+
// inserters
44+
BodyInserter<Publisher<String>, ReactiveHttpOutputMessage> inserter1 = BodyInserters
45+
.fromPublisher(Subscriber::onComplete, String.class);
46+
47+
48+
LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
49+
map.add("key1", "value1");
50+
map.add("key2", "value2");
51+
52+
BodyInserter<MultiValueMap<String, ?>, ClientHttpRequest> inserter2 = BodyInserters.fromMultipartData(map);
53+
BodyInserter<String, ReactiveHttpOutputMessage> inserter3 = BodyInserters.fromObject("body");
54+
55+
// responses
56+
WebClient.ResponseSpec response1 = uri1
57+
.body(inserter3)
58+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
59+
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
60+
.acceptCharset(Charset.forName("UTF-8"))
61+
.ifNoneMatch("*")
62+
.ifModifiedSince(ZonedDateTime.now())
63+
.retrieve();
64+
WebClient.ResponseSpec response2 = requestSpec2.retrieve();
65+
66+
}
67+
68+
private WebClient createWebClient() {
69+
return WebClient.create();
70+
}
71+
72+
private WebClient createWebClientWithServerURL() {
73+
return WebClient.create("http://localhost:8081");
74+
}
75+
76+
private WebClient createWebClientWithServerURLAndDefaultValues() {
77+
return WebClient
78+
.builder()
79+
.baseUrl("http://localhost:8081")
80+
.defaultCookie("cookieKey", "cookieValue")
81+
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
82+
.defaultUriVariables(Collections.singletonMap("url", "http://localhost:8080"))
83+
.build();
84+
}
85+
86+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.web.client;
2+
3+
import com.baeldung.Spring5Application;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.boot.web.server.LocalServerPort;
8+
import org.springframework.test.context.junit4.SpringRunner;
9+
import org.springframework.test.web.reactive.server.WebTestClient;
10+
import org.springframework.web.reactive.function.server.RequestPredicates;
11+
import org.springframework.web.reactive.function.server.RouterFunction;
12+
import org.springframework.web.reactive.function.server.RouterFunctions;
13+
import org.springframework.web.reactive.function.server.ServerResponse;
14+
import org.springframework.web.server.WebHandler;
15+
import reactor.core.publisher.Mono;
16+
17+
@RunWith(SpringRunner.class)
18+
@SpringBootTest(classes = Spring5Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
19+
public class WebTestClientTest {
20+
21+
@LocalServerPort
22+
private int port;
23+
24+
private final RouterFunction ROUTER_FUNCTION = RouterFunctions.route(
25+
RequestPredicates.GET("/resource"),
26+
request -> ServerResponse.ok().build()
27+
);
28+
private final WebHandler WEB_HANDLER = exchange -> Mono.empty();
29+
30+
@Test
31+
public void testWebTestClientWithServerWebHandler() {
32+
WebTestClient.bindToWebHandler(WEB_HANDLER).build();
33+
}
34+
35+
@Test
36+
public void testWebTestClientWithRouterFunction() {
37+
WebTestClient
38+
.bindToRouterFunction(ROUTER_FUNCTION)
39+
.build().get().uri("/resource")
40+
.exchange()
41+
.expectStatus().isOk()
42+
.expectBody().isEmpty();
43+
}
44+
45+
@Test
46+
public void testWebTestClientWithServerURL() {
47+
WebTestClient
48+
.bindToServer()
49+
.baseUrl("http://localhost:" + port)
50+
.build()
51+
.get()
52+
.uri("/resource")
53+
.exchange()
54+
.expectStatus().is4xxClientError()
55+
.expectBody();
56+
}
57+
58+
}

0 commit comments

Comments
 (0)