Add body(Object) method to ServerResponse.BodyBuilder
This method introduces a new body(Object) to ServerResponse, a shortcut to body(BodyInserters.fromObject(Object)). Note that in the implementation of the method, an `instanceof` check is performed to make sure that the passed argument is not a `Publisher`, as users should call `body(Publisher, Class)` for sending a reactive type. This Publisher-check is also done in the `WebClient`, for the same reasons. Issue: SPR-15461
This commit is contained in:
@@ -116,6 +116,14 @@ public class DefaultWebClientTests {
|
||||
verifyNoMoreInteractions(this.exchangeFunction);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void bodyObjectPublisher() throws Exception {
|
||||
Mono<Void> mono = Mono.empty();
|
||||
WebClient client = builder().build();
|
||||
|
||||
client.post().uri("http://example.com").body(mono);
|
||||
}
|
||||
|
||||
|
||||
private WebClient.Builder builder() {
|
||||
return WebClient.builder().baseUrl("/base").exchangeFunction(this.exchangeFunction);
|
||||
|
||||
@@ -35,9 +35,8 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -295,4 +294,12 @@ public class DefaultServerResponseBuilderTests {
|
||||
StepVerifier.create(response.getBody()).expectComplete().verify();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void bodyObjectPublisher() throws Exception {
|
||||
Mono<Void> mono = Mono.empty();
|
||||
|
||||
ServerResponse.ok().body(mono);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -25,7 +25,6 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.path;
|
||||
import static org.springframework.web.reactive.function.server.RouterFunctions.nest;
|
||||
@@ -82,11 +81,11 @@ public class NestedRouteIntegrationTests extends AbstractRouterFunctionIntegrati
|
||||
private static class NestedHandler {
|
||||
|
||||
public Mono<ServerResponse> bar(ServerRequest request) {
|
||||
return ServerResponse.ok().body(fromObject("bar"));
|
||||
return ServerResponse.ok().body("bar");
|
||||
}
|
||||
|
||||
public Mono<ServerResponse> baz(ServerRequest request) {
|
||||
return ServerResponse.ok().body(fromObject("baz"));
|
||||
return ServerResponse.ok().body("baz");
|
||||
}
|
||||
|
||||
public Mono<ServerResponse> variables(ServerRequest request) {
|
||||
|
||||
Reference in New Issue
Block a user