diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java index 7903fcfaa43..65e2e366149 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java @@ -266,18 +266,6 @@ class DefaultWebTestClient implements WebTestClient { return this; } - @Override - public RequestHeadersSpec body(Object producer, Class elementClass) { - this.bodySpec.body(producer, elementClass); - return this; - } - - @Override - public RequestHeadersSpec body(Object producer, ParameterizedTypeReference elementTypeRef) { - this.bodySpec.body(producer, elementTypeRef); - return this; - } - @Override public > RequestHeadersSpec body(S publisher, Class elementClass) { this.bodySpec.body(publisher, elementClass); @@ -290,6 +278,18 @@ class DefaultWebTestClient implements WebTestClient { return this; } + @Override + public RequestHeadersSpec body(Object producer, Class elementClass) { + this.bodySpec.body(producer, elementClass); + return this; + } + + @Override + public RequestHeadersSpec body(Object producer, ParameterizedTypeReference elementTypeRef) { + this.bodySpec.body(producer, elementTypeRef); + return this; + } + @Override public RequestHeadersSpec body(BodyInserter inserter) { this.bodySpec.body(inserter); diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java index 4ded01a40f9..c88dfce3d82 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java @@ -644,6 +644,27 @@ public interface WebTestClient { */ RequestHeadersSpec bodyValue(Object body); + /** + * Set the body of the request to the given asynchronous {@code Publisher}. + * @param publisher the request body data + * @param elementClass the class of elements contained in the publisher + * @param the type of the elements contained in the publisher + * @param the type of the {@code Publisher} + * @return spec for decoding the response + */ + > RequestHeadersSpec body(S publisher, Class elementClass); + + /** + * Set the body of the request to the given asynchronous {@code Publisher}. + * @param publisher the request body data + * @param elementTypeRef the type reference of elements contained in the publisher + * @param the type of the elements contained in the publisher + * @param the type of the {@code Publisher} + * @return spec for decoding the response + * @since 5.2 + */ + > RequestHeadersSpec body(S publisher, ParameterizedTypeReference elementTypeRef); + /** * Set the body of the request to the given producer. * @param producer the producer to write to the request. This must be a @@ -666,27 +687,6 @@ public interface WebTestClient { */ RequestHeadersSpec body(Object producer, ParameterizedTypeReference elementTypeRef); - /** - * Set the body of the request to the given asynchronous {@code Publisher}. - * @param publisher the request body data - * @param elementClass the class of elements contained in the publisher - * @param the type of the elements contained in the publisher - * @param the type of the {@code Publisher} - * @return spec for decoding the response - */ - > RequestHeadersSpec body(S publisher, Class elementClass); - - /** - * Set the body of the request to the given asynchronous {@code Publisher}. - * @param publisher the request body data - * @param elementTypeRef the type reference of elements contained in the publisher - * @param the type of the elements contained in the publisher - * @param the type of the {@code Publisher} - * @return spec for decoding the response - * @since 5.2 - */ - > RequestHeadersSpec body(S publisher, ParameterizedTypeReference elementTypeRef); - /** * Set the body of the request to the given {@code BodyInserter}. * @param inserter the inserter diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java index 6cd90185bcc..0bbdb933304 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java @@ -293,18 +293,6 @@ class DefaultWebClient implements WebClient { return this; } - @Override - public RequestHeadersSpec body(Object producer, Class elementClass) { - this.inserter = BodyInserters.fromProducer(producer, elementClass); - return this; - } - - @Override - public RequestHeadersSpec body(Object producer, ParameterizedTypeReference elementTypeRef) { - this.inserter = BodyInserters.fromProducer(producer, elementTypeRef); - return this; - } - @Override public > RequestHeadersSpec body( P publisher, ParameterizedTypeReference elementTypeRef) { @@ -318,6 +306,18 @@ class DefaultWebClient implements WebClient { return this; } + @Override + public RequestHeadersSpec body(Object producer, Class elementClass) { + this.inserter = BodyInserters.fromProducer(producer, elementClass); + return this; + } + + @Override + public RequestHeadersSpec body(Object producer, ParameterizedTypeReference elementTypeRef) { + this.inserter = BodyInserters.fromProducer(producer, elementTypeRef); + return this; + } + @Override public RequestHeadersSpec body(BodyInserter inserter) { this.inserter = inserter; diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java index fc0cd709023..6b64ab84c7b 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java @@ -549,6 +549,41 @@ public interface WebClient { */ RequestHeadersSpec bodyValue(Object body); + /** + * A shortcut for {@link #body(BodyInserter)} with a + * {@linkplain BodyInserters#fromPublisher Publisher inserter}. + * For example: + *

+		 * Mono<Person> personMono = ... ;
+		 *
+		 * Mono<Void> result = client.post()
+		 *     .uri("/persons/{id}", id)
+		 *     .contentType(MediaType.APPLICATION_JSON)
+		 *     .body(personMono, Person.class)
+		 *     .retrieve()
+		 *     .bodyToMono(Void.class);
+		 * 
+ * @param publisher the {@code Publisher} to write to the request + * @param elementClass the class of elements contained in the publisher + * @param the type of the elements contained in the publisher + * @param

the type of the {@code Publisher} + * @return this builder + */ + > RequestHeadersSpec body(P publisher, Class elementClass); + + /** + * A variant of {@link #body(Publisher, Class)} that allows providing + * element type information that includes generics via a + * {@link ParameterizedTypeReference}. + * @param publisher the {@code Publisher} to write to the request + * @param elementTypeRef the type reference of elements contained in the publisher + * @param the type of the elements contained in the publisher + * @param

the type of the {@code Publisher} + * @return this builder + */ + > RequestHeadersSpec body(P publisher, + ParameterizedTypeReference elementTypeRef); + /** * A shortcut for {@link #body(BodyInserter)} with a * {@linkplain BodyInserters#fromProducer inserter}. @@ -585,41 +620,6 @@ public interface WebClient { */ RequestHeadersSpec body(Object producer, ParameterizedTypeReference elementTypeRef); - /** - * A shortcut for {@link #body(BodyInserter)} with a - * {@linkplain BodyInserters#fromPublisher Publisher inserter}. - * For example: - *

-		 * Mono<Person> personMono = ... ;
-		 *
-		 * Mono<Void> result = client.post()
-		 *     .uri("/persons/{id}", id)
-		 *     .contentType(MediaType.APPLICATION_JSON)
-		 *     .body(personMono, Person.class)
-		 *     .retrieve()
-		 *     .bodyToMono(Void.class);
-		 * 
- * @param publisher the {@code Publisher} to write to the request - * @param elementClass the class of elements contained in the publisher - * @param the type of the elements contained in the publisher - * @param

the type of the {@code Publisher} - * @return this builder - */ - > RequestHeadersSpec body(P publisher, Class elementClass); - - /** - * A variant of {@link #body(Publisher, Class)} that allows providing - * element type information that includes generics via a - * {@link ParameterizedTypeReference}. - * @param publisher the {@code Publisher} to write to the request - * @param elementTypeRef the type reference of elements contained in the publisher - * @param the type of the elements contained in the publisher - * @param

the type of the {@code Publisher} - * @return this builder - */ - > RequestHeadersSpec body(P publisher, - ParameterizedTypeReference elementTypeRef); - /** * Set the body of the request using the given body inserter. * {@link BodyInserters} provides access to built-in implementations of diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java index 87b51269b10..39f57133cdc 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java @@ -234,30 +234,6 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder { .map(entityResponse -> entityResponse); } - @Override - public Mono body(Object producer, Class elementClass) { - return new DefaultEntityResponseBuilder<>(producer, - BodyInserters.fromProducer(producer, elementClass)) - .status(this.statusCode) - .headers(this.headers) - .cookies(cookies -> cookies.addAll(this.cookies)) - .hints(hints -> hints.putAll(this.hints)) - .build() - .map(entityResponse -> entityResponse); - } - - @Override - public Mono body(Object producer, ParameterizedTypeReference elementTypeRef) { - return new DefaultEntityResponseBuilder<>(producer, - BodyInserters.fromProducer(producer, elementTypeRef)) - .status(this.statusCode) - .headers(this.headers) - .cookies(cookies -> cookies.addAll(this.cookies)) - .hints(hints -> hints.putAll(this.hints)) - .build() - .map(entityResponse -> entityResponse); - } - @Override public > Mono body(P publisher, Class elementClass) { return new DefaultEntityResponseBuilder<>(publisher, @@ -284,9 +260,27 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder { } @Override - @Deprecated - public Mono syncBody(Object body) { - return bodyValue(body); + public Mono body(Object producer, Class elementClass) { + return new DefaultEntityResponseBuilder<>(producer, + BodyInserters.fromProducer(producer, elementClass)) + .status(this.statusCode) + .headers(this.headers) + .cookies(cookies -> cookies.addAll(this.cookies)) + .hints(hints -> hints.putAll(this.hints)) + .build() + .map(entityResponse -> entityResponse); + } + + @Override + public Mono body(Object producer, ParameterizedTypeReference elementTypeRef) { + return new DefaultEntityResponseBuilder<>(producer, + BodyInserters.fromProducer(producer, elementTypeRef)) + .status(this.statusCode) + .headers(this.headers) + .cookies(cookies -> cookies.addAll(this.cookies)) + .hints(hints -> hints.putAll(this.hints)) + .build() + .map(entityResponse -> entityResponse); } @Override @@ -295,6 +289,12 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder { new BodyInserterResponse<>(this.statusCode, this.headers, this.cookies, inserter, this.hints)); } + @Override + @Deprecated + public Mono syncBody(Object body) { + return bodyValue(body); + } + @Override public Mono render(String name, Object... modelAttributes) { return new DefaultRenderingResponseBuilder(name) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerResponse.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerResponse.java index 79ff425f1df..cdf620c4265 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerResponse.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerResponse.java @@ -410,6 +410,31 @@ public interface ServerResponse { */ Mono bodyValue(Object body); + /** + * Set the body of the response to the given asynchronous {@code Publisher} and return it. + * This convenience method combines {@link #body(BodyInserter)} and + * {@link BodyInserters#fromPublisher(Publisher, Class)}. + * @param publisher the {@code Publisher} to write to the response + * @param elementClass the class of elements contained in the publisher + * @param the type of the elements contained in the publisher + * @param

the type of the {@code Publisher} + * @return the built response + */ + > Mono body(P publisher, Class elementClass); + + /** + * Set the body of the response to the given asynchronous {@code Publisher} and return it. + * This convenience method combines {@link #body(BodyInserter)} and + * {@link BodyInserters#fromPublisher(Publisher, ParameterizedTypeReference)}. + * @param publisher the {@code Publisher} to write to the response + * @param elementTypeRef a type reference describing the elements contained in the publisher + * @param the type of the elements contained in the publisher + * @param

the type of the {@code Publisher} + * @return the built response + */ + > Mono body(P publisher, + ParameterizedTypeReference elementTypeRef); + /** * Set the body of the response to the given asynchronous {@code Publisher} and return it. * This convenience method combines {@link #body(BodyInserter)} and @@ -436,31 +461,6 @@ public interface ServerResponse { */ Mono body(Object producer, ParameterizedTypeReference elementTypeRef); - /** - * Set the body of the response to the given asynchronous {@code Publisher} and return it. - * This convenience method combines {@link #body(BodyInserter)} and - * {@link BodyInserters#fromPublisher(Publisher, Class)}. - * @param publisher the {@code Publisher} to write to the response - * @param elementClass the class of elements contained in the publisher - * @param the type of the elements contained in the publisher - * @param

the type of the {@code Publisher} - * @return the built response - */ - > Mono body(P publisher, Class elementClass); - - /** - * Set the body of the response to the given asynchronous {@code Publisher} and return it. - * This convenience method combines {@link #body(BodyInserter)} and - * {@link BodyInserters#fromPublisher(Publisher, ParameterizedTypeReference)}. - * @param publisher the {@code Publisher} to write to the response - * @param elementTypeRef a type reference describing the elements contained in the publisher - * @param the type of the elements contained in the publisher - * @param

the type of the {@code Publisher} - * @return the built response - */ - > Mono body(P publisher, - ParameterizedTypeReference elementTypeRef); - /** * Set the body of the response to the given {@code BodyInserter} and return it. * @param inserter the {@code BodyInserter} that writes to the response