Skip to content

Commit d6b5c20

Browse files
committed
Generalize RSocketRequester data methods
Replace the Publisher argument in RequestSpec's data(Publisher, Class<T>) and data(Publisher, ParameterizedTypeReference<T>) methods with Object thus allowing any reactive type known to the ReactiveAdapterRegistry to be passed in directly rather than adapted to Publisher first.
1 parent b3d40b3 commit d6b5c20

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed

spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketRequester.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,21 @@ public ResponseSpec data(Object data) {
152152
}
153153

154154
@Override
155-
public <T, P extends Publisher<T>> ResponseSpec data(P publisher, Class<T> dataType) {
156-
Assert.notNull(publisher, "'publisher' must not be null");
157-
Assert.notNull(dataType, "'dataType' must not be null");
158-
return toResponseSpec(publisher, ResolvableType.forClass(dataType));
155+
public ResponseSpec data(Object producer, Class<?> elementType) {
156+
Assert.notNull(producer, "'producer' must not be null");
157+
Assert.notNull(elementType, "'dataType' must not be null");
158+
ReactiveAdapter adapter = strategies.reactiveAdapterRegistry().getAdapter(producer.getClass());
159+
Assert.notNull(adapter, "'producer' type is unknown to ReactiveAdapterRegistry");
160+
return toResponseSpec(adapter.toPublisher(producer), ResolvableType.forClass(elementType));
159161
}
160162

161163
@Override
162-
public <T, P extends Publisher<T>> ResponseSpec data(P publisher, ParameterizedTypeReference<T> dataTypeRef) {
163-
Assert.notNull(publisher, "'publisher' must not be null");
164+
public ResponseSpec data(Object producer, ParameterizedTypeReference<?> dataTypeRef) {
165+
Assert.notNull(producer, "'producer' must not be null");
164166
Assert.notNull(dataTypeRef, "'dataTypeRef' must not be null");
165-
return toResponseSpec(publisher, ResolvableType.forType(dataTypeRef));
167+
ReactiveAdapter adapter = strategies.reactiveAdapterRegistry().getAdapter(producer.getClass());
168+
Assert.notNull(adapter, "'producer' type is unknown to ReactiveAdapterRegistry");
169+
return toResponseSpec(adapter.toPublisher(producer), ResolvableType.forType(dataTypeRef));
166170
}
167171

168172
private ResponseSpec toResponseSpec(Object input, ResolvableType dataType) {

spring-messaging/src/main/java/org/springframework/messaging/rsocket/RSocketRequester.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -200,41 +200,41 @@ interface RequestSpec {
200200
RequestSpec metadata(Object metadata, MimeType mimeType);
201201

202202
/**
203-
* Provide request payload data. The given Object may be a synchronous
204-
* value, or a {@link Publisher} of values, or another async type that's
205-
* registered in the configured {@link ReactiveAdapterRegistry}.
206-
* <p>For multi-valued Publishers, prefer using
207-
* {@link #data(Publisher, Class)} or
208-
* {@link #data(Publisher, ParameterizedTypeReference)} since that makes
209-
* it possible to find a compatible {@code Encoder} up front vs looking
210-
* it up on every value.
203+
* Provide payload data. The data can be one of the following:
204+
* <ul>
205+
* <li>Concrete value
206+
* <li>{@link Publisher} of value(s)
207+
* <li>Any other producer of value(s) that can be adapted to a
208+
* {@link Publisher} via {@link ReactiveAdapterRegistry}
209+
* </ul>
211210
* @param data the Object to use for payload data
212211
* @return spec for declaring the expected response
213212
*/
214213
ResponseSpec data(Object data);
215214

216215
/**
217-
* Provide a {@link Publisher} of value(s) for request payload data.
218-
* <p>Publisher semantics determined through the configured
219-
* {@link ReactiveAdapterRegistry} influence which of the 4 RSocket
220-
* interactions to use. Publishers with unknown semantics are treated
221-
* as multi-valued. Consider registering a reactive type adapter, or
222-
* passing {@code Mono.from(publisher)}.
223-
* <p>If the publisher completes empty, possibly {@code Publisher<Void>},
224-
* the request will have an empty data Payload.
225-
* @param publisher source of payload data value(s)
226-
* @param dataType the type of values to be published
227-
* @param <T> the type of element values
228-
* @param <P> the type of publisher
216+
* Alternative of {@link #data(Object)} that accepts not only a producer
217+
* of value(s) but also a hint for the types of values that will be
218+
* produced. The class hint is used to find a compatible {@code Encoder}
219+
* once, up front, and used for all values.
220+
* @param producer the source of payload data value(s). This must be a
221+
* {@link Publisher} or another producer adaptable to a
222+
* {@code Publisher} via {@link ReactiveAdapterRegistry}
223+
* @param elementType the type of values to be produced
229224
* @return spec for declaring the expected response
230225
*/
231-
<T, P extends Publisher<T>> ResponseSpec data(P publisher, Class<T> dataType);
226+
ResponseSpec data(Object producer, Class<?> elementType);
232227

233228
/**
234-
* Variant of {@link #data(Publisher, Class)} for when the dataType has
235-
* to have a generic type. See {@link ParameterizedTypeReference}.
229+
* Alternative of {@link #data(Object, Class)} but with a
230+
* {@link ParameterizedTypeReference} hint which can provide generic
231+
* type information.
232+
* @param producer the source of payload data value(s). This must be a
233+
* {@link Publisher} or another producer adaptable to a
234+
* {@code Publisher} via {@link ReactiveAdapterRegistry}
235+
* @param elementTypeRef the type of values to be produced
236236
*/
237-
<T, P extends Publisher<T>> ResponseSpec data(P publisher, ParameterizedTypeReference<T> dataTypeRef);
237+
ResponseSpec data(Object producer, ParameterizedTypeReference<?> elementTypeRef);
238238
}
239239

240240

0 commit comments

Comments
 (0)