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:
Arjen Poutsma
2017-04-19 16:40:23 +02:00
parent b897f96e0f
commit 30f61e0c07
7 changed files with 59 additions and 19 deletions

View File

@@ -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);

View File

@@ -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);
}
}

View File

@@ -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) {