|
| 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 | +} |
0 commit comments