Move toEntity(List) from WebClient.ResponseSpec to ClientResponse
This commit moves `toEntity(Class<T>)` and `toEntityList(Class<T>)`
from WebClient.ResponseSpec to ClientResponse. The main reason for doing
so is that the newly introduced `onStatus` method (see
2f9bd6e075) does not apply to these two
methods, and the result would be confusing. Also, `ClientResponse` and
`ResponseEntity` represent the same data: status code, headers, and a
body.
Issue: SPR-15724
This commit is contained in:
@@ -158,7 +158,7 @@ public class WebClientIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonStringRetrieveEntity() throws Exception {
|
||||
public void jsonStringExchangeEntity() throws Exception {
|
||||
String content = "{\"bar\":\"barbar\",\"foo\":\"foofoo\"}";
|
||||
this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json")
|
||||
.setBody(content));
|
||||
@@ -166,8 +166,8 @@ public class WebClientIntegrationTests {
|
||||
Mono<ResponseEntity<String>> result = this.webClient.get()
|
||||
.uri("/json")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.retrieve()
|
||||
.toEntity(String.class);
|
||||
.exchange()
|
||||
.flatMap(response -> response.toEntity(String.class));
|
||||
|
||||
StepVerifier.create(result)
|
||||
.consumeNextWith(entity -> {
|
||||
@@ -186,15 +186,15 @@ public class WebClientIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonStringRetrieveEntityList() throws Exception {
|
||||
public void jsonStringExchangeEntityList() 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()
|
||||
.toEntityList(Pojo.class);
|
||||
.exchange()
|
||||
.flatMap(response -> response.toEntityList(Pojo.class));
|
||||
|
||||
StepVerifier.create(result)
|
||||
.consumeNextWith(entity -> {
|
||||
@@ -412,14 +412,14 @@ public class WebClientIntegrationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrieveToEntityNotFound() throws Exception {
|
||||
public void exchangeToEntityNotFound() throws Exception {
|
||||
this.server.enqueue(new MockResponse().setResponseCode(404)
|
||||
.setHeader("Content-Type", "text/plain").setBody("Not Found"));
|
||||
|
||||
Mono<ResponseEntity<String>> result = this.webClient.get()
|
||||
.uri("/greeting?name=Spring")
|
||||
.retrieve()
|
||||
.toEntity(String.class);
|
||||
.exchange()
|
||||
.flatMap(response -> response.toEntity(String.class));
|
||||
|
||||
StepVerifier.create(result)
|
||||
.consumeNextWith(response -> assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()))
|
||||
@@ -521,21 +521,6 @@ public class WebClientIntegrationTests {
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retrieveNoContent() throws Exception {
|
||||
this.server.enqueue(new MockResponse().setHeader("Content-Length", "0"));
|
||||
|
||||
Mono<ResponseEntity<Void>> result = this.webClient.get()
|
||||
.uri("/noContent")
|
||||
.retrieve()
|
||||
.toEntity(Void.class);
|
||||
|
||||
StepVerifier.create(result).assertNext(r -> {
|
||||
assertFalse(r.hasBody());
|
||||
assertTrue(r.getStatusCode().is2xxSuccessful());
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class MyException extends RuntimeException {
|
||||
|
||||
@@ -20,7 +20,8 @@ import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Answers
|
||||
import org.mockito.Mock
|
||||
import org.mockito.Mockito.*
|
||||
import org.mockito.Mockito.times
|
||||
import org.mockito.Mockito.verify
|
||||
import org.mockito.junit.MockitoJUnitRunner
|
||||
|
||||
/**
|
||||
@@ -46,5 +47,17 @@ class ClientResponseExtensionsTests {
|
||||
verify(response, times(1)).bodyToFlux(Foo::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toEntity with reified type parameters`() {
|
||||
response.toEntity<Foo>()
|
||||
verify(response, times(1)).toEntity(Foo::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ResponseSpec#toEntityList with reified type parameters`() {
|
||||
response.toEntityList<Foo>()
|
||||
verify(response, times(1)).toEntityList(Foo::class.java)
|
||||
}
|
||||
|
||||
class Foo
|
||||
}
|
||||
|
||||
@@ -21,7 +21,8 @@ import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Answers
|
||||
import org.mockito.Mock
|
||||
import org.mockito.Mockito.*
|
||||
import org.mockito.Mockito.times
|
||||
import org.mockito.Mockito.verify
|
||||
import org.mockito.junit.MockitoJUnitRunner
|
||||
import org.reactivestreams.Publisher
|
||||
|
||||
@@ -59,17 +60,5 @@ class WebClientExtensionsTests {
|
||||
verify(responseSpec, times(1)).bodyToFlux(Foo::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ResponseSpec#toEntity with reified type parameters`() {
|
||||
responseSpec.toEntity<Foo>()
|
||||
verify(responseSpec, times(1)).toEntity(Foo::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ResponseSpec#toEntityList with reified type parameters`() {
|
||||
responseSpec.toEntityList<Foo>()
|
||||
verify(responseSpec, times(1)).toEntityList(Foo::class.java)
|
||||
}
|
||||
|
||||
class Foo
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user