From 5bb8c47b147d38a58ddcbcc86f1acd3afc33ba54 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Mon, 9 Sep 2019 00:08:47 +0200 Subject: [PATCH] Rename fromObject to fromValue in BodyInserters Closes gh-23587 --- .../web/reactive/server/WebTestClient.java | 4 +-- .../web/reactive/function/BodyInserters.java | 29 ++++++++++++++++--- .../function/client/DefaultWebClient.java | 2 +- .../reactive/function/client/WebClient.java | 8 ++--- .../server/DefaultServerResponseBuilder.java | 2 +- .../function/server/EntityResponse.java | 2 +- .../function/server/ServerResponse.java | 4 +-- .../server/ServerResponseExtensions.kt | 2 +- .../reactive/function/BodyInsertersTests.java | 6 ++-- .../DefaultServerResponseBuilderTests.java | 3 +- .../function/server/RouterFunctionTests.java | 5 ++-- 11 files changed, 43 insertions(+), 24 deletions(-) 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 21d370a7b6..7111b8d528 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 @@ -704,8 +704,8 @@ public interface WebTestClient { RequestHeadersSpec body(BodyInserter inserter); /** - * Shortcut for {@link #body(BodyInserter)} with an - * {@linkplain BodyInserters#fromObject Object inserter}. + * Shortcut for {@link #body(BodyInserter)} with a + * {@linkplain BodyInserters#fromValue value inserter}. * As of 5.2 this method delegates to {@link #bodyValue(Object)}. * @deprecated as of Spring Framework 5.2 in favor of {@link #bodyValue(Object)} */ diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyInserters.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyInserters.java index 4db8f530cc..964372da49 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyInserters.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyInserters.java @@ -76,6 +76,28 @@ public abstract class BodyInserters { return (BodyInserter) EMPTY_INSERTER; } + /** + * Inserter to write the given value. + *

Alternatively, consider using the {@code bodyValue(Object)} shortcuts on + * {@link org.springframework.web.reactive.function.client.WebClient WebClient} and + * {@link org.springframework.web.reactive.function.server.ServerResponse ServerResponse}. + * @param body the value to write + * @param the type of the body + * @return the inserter to write a single value + * @throws IllegalArgumentException if {@code body} is a {@link Publisher} or an + * instance of a type supported by {@link ReactiveAdapterRegistry#getSharedInstance()}, + * for which {@link #fromPublisher(Publisher, Class)} or + * {@link #fromProducer(Object, Class)} should be used. + * @see #fromPublisher(Publisher, Class) + * @see #fromProducer(Object, Class) + */ + public static BodyInserter fromValue(T body) { + Assert.notNull(body, "'body' must not be null"); + Assert.isNull(registry.getAdapter(body.getClass()), "'body' should be an object, for reactive types use a variant specifying a publisher/producer and its related element type"); + return (message, context) -> + writeWithMessageWriters(message, context, Mono.just(body), ResolvableType.forInstance(body), null); + } + /** * Inserter to write the given object. *

Alternatively, consider using the {@code bodyValue(Object)} shortcuts on @@ -90,12 +112,11 @@ public abstract class BodyInserters { * {@link #fromProducer(Object, Class)} should be used. * @see #fromPublisher(Publisher, Class) * @see #fromProducer(Object, Class) + * @deprecated As of Spring Framework 5.2, in favor of {@link #fromValue(T)}. */ + @Deprecated public static BodyInserter fromObject(T body) { - Assert.notNull(body, "'body' must not be null"); - Assert.isNull(registry.getAdapter(body.getClass()), "'body' should be an object, for reactive types use a variant specifying a publisher/producer and its related element type"); - return (message, context) -> - writeWithMessageWriters(message, context, Mono.just(body), ResolvableType.forInstance(body), null); + return fromValue(body); } /** 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 0f958e4bf7..43e8a2cb78 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 @@ -289,7 +289,7 @@ class DefaultWebClient implements WebClient { @Override public RequestHeadersSpec bodyValue(Object body) { - this.inserter = BodyInserters.fromObject(body); + this.inserter = BodyInserters.fromValue(body); return this; } 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 f20b85f0e2..5b6a6359dc 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 @@ -517,8 +517,8 @@ public interface WebClient { RequestBodySpec contentType(MediaType contentType); /** - * Shortcut for {@link #body(BodyInserter)} with an - * {@linkplain BodyInserters#fromObject Object inserter}. + * Shortcut for {@link #body(BodyInserter)} with a + * {@linkplain BodyInserters#fromValue value inserter}. * For example: *

 		 * Person person = ... ;
@@ -608,8 +608,8 @@ public interface WebClient {
 		RequestHeadersSpec body(BodyInserter inserter);
 
 		/**
-		 * Shortcut for {@link #body(BodyInserter)} with an
-		 * {@linkplain BodyInserters#fromObject Object inserter}.
+		 * Shortcut for {@link #body(BodyInserter)} with a
+		 * {@linkplain BodyInserters#fromValue value inserter}.
 		 * As of 5.2 this method delegates to {@link #bodyValue(Object)}.
 		 * @deprecated as of Spring Framework 5.2 in favor of {@link #bodyValue(Object)}
 		 */
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 fd93bf3137..d76651f93e 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
@@ -225,7 +225,7 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
 
 	@Override
 	public Mono bodyValue(Object body) {
-		return initBuilder(body, BodyInserters.fromObject(body));
+		return initBuilder(body, BodyInserters.fromValue(body));
 	}
 
 	@Override
diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/EntityResponse.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/EntityResponse.java
index 152b038770..2091231510 100644
--- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/EntityResponse.java
+++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/EntityResponse.java
@@ -69,7 +69,7 @@ public interface EntityResponse extends ServerResponse {
 	 * @return the created builder
 	 */
 	static  Builder fromObject(T body) {
-		return new DefaultEntityResponseBuilder<>(body, BodyInserters.fromObject(body));
+		return new DefaultEntityResponseBuilder<>(body, BodyInserters.fromValue(body));
 	}
 
 	/**
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 242d3a2421..cd4861a4a8 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
@@ -399,8 +399,8 @@ public interface ServerResponse {
 
 		/**
 		 * Set the body of the response to the given {@code Object} and return it.
-		 * This is a shortcut for using a {@link #body(BodyInserter)} with an
-		 * {@linkplain BodyInserters#fromObject Object inserter}.
+		 * This is a shortcut for using a {@link #body(BodyInserter)} with a
+		 * {@linkplain BodyInserters#fromValue value inserter}.
 		 * @param body the body of the response
 		 * @return the built response
 		 * @throws IllegalArgumentException if {@code body} is a
diff --git a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensions.kt b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensions.kt
index 392ef989a0..826070773d 100644
--- a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensions.kt
+++ b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensions.kt
@@ -54,7 +54,7 @@ inline fun  ServerResponse.BodyBuilder.body(producer: Any): Mon
  *
  * Set the body of the response to the given {@code Object} and return it.
  * This convenience method combines [body] and
- * [org.springframework.web.reactive.function.BodyInserters.fromObject].
+ * [org.springframework.web.reactive.function.BodyInserters.fromValue].
  * @param body the body of the response
  * @return the built response
  * @throws IllegalArgumentException if `body` is a [Publisher] or an
diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/BodyInsertersTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/BodyInsertersTests.java
index 8f31694a4f..edcdb983ee 100644
--- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/BodyInsertersTests.java
+++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/BodyInsertersTests.java
@@ -117,7 +117,7 @@ public class BodyInsertersTests {
 	@Test
 	public void ofString() {
 		String body = "foo";
-		BodyInserter inserter = BodyInserters.fromObject(body);
+		BodyInserter inserter = BodyInserters.fromValue(body);
 
 		MockServerHttpResponse response = new MockServerHttpResponse();
 		Mono result = inserter.insert(response, this.context);
@@ -134,7 +134,7 @@ public class BodyInsertersTests {
 	@Test
 	public void ofObject() {
 		User body = new User("foo", "bar");
-		BodyInserter inserter = BodyInserters.fromObject(body);
+		BodyInserter inserter = BodyInserters.fromValue(body);
 		MockServerHttpResponse response = new MockServerHttpResponse();
 		Mono result = inserter.insert(response, this.context);
 		StepVerifier.create(result).expectComplete().verify();
@@ -148,7 +148,7 @@ public class BodyInsertersTests {
 	@Test
 	public void ofObjectWithHints() {
 		User body = new User("foo", "bar");
-		BodyInserter inserter = BodyInserters.fromObject(body);
+		BodyInserter inserter = BodyInserters.fromValue(body);
 		this.hints.put(JSON_VIEW_HINT, SafeToSerialize.class);
 		MockServerHttpResponse response = new MockServerHttpResponse();
 		Mono result = inserter.insert(response, this.context);
diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java
index eb8640dea5..d687a29f8c 100644
--- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java
+++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java
@@ -41,7 +41,6 @@ import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse
 import org.springframework.mock.web.test.server.MockServerWebExchange;
 import org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.MultiValueMap;
-import org.springframework.web.reactive.function.BodyInserters;
 import org.springframework.web.reactive.result.view.ViewResolver;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -315,7 +314,7 @@ public class DefaultServerResponseBuilderTests {
 
 		serverResponse = ServerResponse.ok()
 				.cookie(ResponseCookie.from("foo", "bar").build())
-				.body(BodyInserters.fromObject("body"));
+				.bodyValue("body");
 
 
 		assertThat(serverResponse.block().cookies().isEmpty()).isFalse();
diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionTests.java
index d3d89e8263..19757a9476 100644
--- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionTests.java
+++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RouterFunctionTests.java
@@ -21,7 +21,6 @@ import reactor.core.publisher.Mono;
 import reactor.test.StepVerifier;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.springframework.web.reactive.function.BodyInserters.fromObject;
 
 /**
  * @author Arjen Poutsma
@@ -49,7 +48,7 @@ public class RouterFunctionTests {
 	@Test
 	public void andOther() {
 		HandlerFunction handlerFunction =
-				request -> ServerResponse.ok().body(fromObject("42"));
+				request -> ServerResponse.ok().bodyValue("42");
 		RouterFunction routerFunction1 = request -> Mono.empty();
 		RouterFunction routerFunction2 =
 				request -> Mono.just(handlerFunction);
@@ -120,7 +119,7 @@ public class RouterFunctionTests {
 
 
 	private Mono handlerMethod(ServerRequest request) {
-		return ServerResponse.ok().body(fromObject("42"));
+		return ServerResponse.ok().bodyValue("42");
 	}
 
 }