Rename fromObject to fromValue in BodyInserters

Closes gh-23587
This commit is contained in:
Sebastien Deleuze 2019-09-09 00:08:47 +02:00
parent 665e8aa51c
commit 5bb8c47b14
11 changed files with 43 additions and 24 deletions

View File

@ -704,8 +704,8 @@ public interface WebTestClient {
RequestHeadersSpec<?> body(BodyInserter<?, ? super ClientHttpRequest> 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)}
*/

View File

@ -76,6 +76,28 @@ public abstract class BodyInserters {
return (BodyInserter<T, ReactiveHttpOutputMessage>) EMPTY_INSERTER;
}
/**
* Inserter to write the given value.
* <p>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 <T> 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 <T> BodyInserter<T, ReactiveHttpOutputMessage> 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.
* <p>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 <T> BodyInserter<T, ReactiveHttpOutputMessage> 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);
}
/**

View File

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

View File

@ -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:
* <p><pre class="code">
* Person person = ... ;
@ -608,8 +608,8 @@ public interface WebClient {
RequestHeadersSpec<?> body(BodyInserter<?, ? super ClientHttpRequest> 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)}
*/

View File

@ -225,7 +225,7 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
@Override
public Mono<ServerResponse> bodyValue(Object body) {
return initBuilder(body, BodyInserters.fromObject(body));
return initBuilder(body, BodyInserters.fromValue(body));
}
@Override

View File

@ -69,7 +69,7 @@ public interface EntityResponse<T> extends ServerResponse {
* @return the created builder
*/
static <T> Builder<T> fromObject(T body) {
return new DefaultEntityResponseBuilder<>(body, BodyInserters.fromObject(body));
return new DefaultEntityResponseBuilder<>(body, BodyInserters.fromValue(body));
}
/**

View File

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

View File

@ -54,7 +54,7 @@ inline fun <reified T : Any> 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

View File

@ -117,7 +117,7 @@ public class BodyInsertersTests {
@Test
public void ofString() {
String body = "foo";
BodyInserter<String, ReactiveHttpOutputMessage> inserter = BodyInserters.fromObject(body);
BodyInserter<String, ReactiveHttpOutputMessage> inserter = BodyInserters.fromValue(body);
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = inserter.insert(response, this.context);
@ -134,7 +134,7 @@ public class BodyInsertersTests {
@Test
public void ofObject() {
User body = new User("foo", "bar");
BodyInserter<User, ReactiveHttpOutputMessage> inserter = BodyInserters.fromObject(body);
BodyInserter<User, ReactiveHttpOutputMessage> inserter = BodyInserters.fromValue(body);
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> 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<User, ReactiveHttpOutputMessage> inserter = BodyInserters.fromObject(body);
BodyInserter<User, ReactiveHttpOutputMessage> inserter = BodyInserters.fromValue(body);
this.hints.put(JSON_VIEW_HINT, SafeToSerialize.class);
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = inserter.insert(response, this.context);

View File

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

View File

@ -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<ServerResponse> handlerFunction =
request -> ServerResponse.ok().body(fromObject("42"));
request -> ServerResponse.ok().bodyValue("42");
RouterFunction<?> routerFunction1 = request -> Mono.empty();
RouterFunction<ServerResponse> routerFunction2 =
request -> Mono.just(handlerFunction);
@ -120,7 +119,7 @@ public class RouterFunctionTests {
private Mono<ServerResponse> handlerMethod(ServerRequest request) {
return ServerResponse.ok().body(fromObject("42"));
return ServerResponse.ok().bodyValue("42");
}
}