Add ResponseSpec to WebClient
Replace the overloaded "retrieve" methods with a single retrieve() + ResponseSpec exposing shortcut methods (bodyToMono, bodyToFlux) mirroring the ClientResponse shortcuts it delegates to. Unlike exchange() however with retrieve() there is no access to other parts of ClientResponse so ResponseSpec exposes additional shortcuts for obtain ResponseEntity<T> or ResponseEntity<List<T>>. Issue: SPR-15294
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
package org.springframework.web.reactive.function.client;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import okhttp3.mockwebserver.MockResponse;
|
||||
import okhttp3.mockwebserver.MockWebServer;
|
||||
@@ -33,6 +35,7 @@ import reactor.test.StepVerifier;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.codec.Pojo;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -142,7 +145,8 @@ public class WebClientIntegrationTests {
|
||||
Mono<String> result = this.webClient.get()
|
||||
.uri("/json")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.retrieveMono(String.class);
|
||||
.retrieve()
|
||||
.bodyToMono(String.class);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNext(content)
|
||||
@@ -155,6 +159,63 @@ public class WebClientIntegrationTests {
|
||||
Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonStringRetrieveEntity() throws Exception {
|
||||
String content = "{\"bar\":\"barbar\",\"foo\":\"foofoo\"}";
|
||||
this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json")
|
||||
.setBody(content));
|
||||
|
||||
Mono<ResponseEntity<String>> result = this.webClient.get()
|
||||
.uri("/json")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.retrieve()
|
||||
.bodyToEntity(String.class);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.consumeNextWith(entity -> {
|
||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||
assertEquals(MediaType.APPLICATION_JSON, entity.getHeaders().getContentType());
|
||||
assertEquals(31, entity.getHeaders().getContentLength());
|
||||
assertEquals(content, entity.getBody());
|
||||
})
|
||||
.expectComplete()
|
||||
.verify(Duration.ofSeconds(3));
|
||||
|
||||
RecordedRequest recordedRequest = server.takeRequest();
|
||||
Assert.assertEquals(1, server.getRequestCount());
|
||||
Assert.assertEquals("/json", recordedRequest.getPath());
|
||||
Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonStringRetrieveEntityList() throws Exception {
|
||||
String content = "[{\"bar\":\"bar1\",\"foo\":\"foo1\"}, {\"bar\":\"bar2\",\"foo\":\"foo2\"}]";
|
||||
this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json").setBody(content));
|
||||
|
||||
Mono<ResponseEntity<List<Pojo>>> result = this.webClient.get()
|
||||
.uri("/json")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.retrieve()
|
||||
.bodyToEntityList(Pojo.class);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.consumeNextWith(entity -> {
|
||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||
assertEquals(MediaType.APPLICATION_JSON, entity.getHeaders().getContentType());
|
||||
assertEquals(58, entity.getHeaders().getContentLength());
|
||||
Pojo pojo1 = new Pojo("foo1", "bar1");
|
||||
Pojo pojo2 = new Pojo("foo2", "bar2");
|
||||
assertEquals(Arrays.asList(pojo1, pojo2), entity.getBody());
|
||||
})
|
||||
.expectComplete()
|
||||
.verify(Duration.ofSeconds(3));
|
||||
|
||||
RecordedRequest recordedRequest = server.takeRequest();
|
||||
Assert.assertEquals(1, server.getRequestCount());
|
||||
Assert.assertEquals("/json", recordedRequest.getPath());
|
||||
Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonStringRetrieveFlux() throws Exception {
|
||||
String content = "{\"bar\":\"barbar\",\"foo\":\"foofoo\"}";
|
||||
@@ -164,7 +225,8 @@ public class WebClientIntegrationTests {
|
||||
Flux<String> result = this.webClient.get()
|
||||
.uri("/json")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.retrieveFlux(String.class);
|
||||
.retrieve()
|
||||
.bodyToFlux(String.class);
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNext(content)
|
||||
|
||||
Reference in New Issue
Block a user