-
Notifications
You must be signed in to change notification settings - Fork 38.9k
Generalize ServerResponse and WebClient body methods #23212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The commit is a follow-up of d6b5c20 which applies the same principle to ServerResponse, WebClient.RequestBodySpec and WebTestClient.RequestBodySpec body methods. It introduces a body(Object) method where the parameter can be: - Concrete value - Publisher of value(s) - Any other producer of value(s) that can be adapted to a Publisher via ReactiveAdapterRegistry Variants with Class and ParameterizedTypeReference parameters are also provided and syncBody(Object) is deprecated. With this change, in Kotlin it is now mandatory to specify explicitly the generic type (for example body<List<Foo>>(body)) in order to invoke the ParameterizedTypeReference shortcut. If it is not specified, the extension is shadowed by the Java body(Object) method which is not able to use Kotlin type inference to resolve the generic type.
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java
Show resolved
Hide resolved
spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyInserters.java
Show resolved
Hide resolved
| */ | ||
| public static <T> BodyInserter<T, ReactiveHttpOutputMessage> fromObject(T body) { | ||
| Assert.notNull(body, "'body' must not be null"); | ||
| ReactiveAdapter adapter = ReactiveAdapterRegistry.getSharedInstance().getAdapter(body.getClass()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the shared instance means if someone tries to register additional adapters, they don't have a way to influence this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we use the hints mechanism to resolve the registry?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rstoyanchev Could you please elaborate? I am not sure to understand.
The recently added body(Object) variant can be confused easily with body(Publisher, Class) forgetting to provide the element type and only running into the IllegalArgumentException at runtime. See gh-23212
This commit adapts to recent changes in Spring Framework. See spring-projects/spring-framework#23212
This commit adapts to recent changes in Spring Framework. See spring-projects/spring-framework#23212
The commit is a follow-up of d6b5c20
which applies the same principle to ServerResponse,
WebClient.RequestBodySpec and WebTestClient.RequestBodySpec body
methods.
It introduces a body(Object) method where the parameter can be:
Publisher via ReactiveAdapterRegistry
Variants with Class and ParameterizedTypeReference parameters are also
provided and syncBody(Object) is deprecated.
With this change, in Kotlin it is now mandatory to specify explicitly
the generic type (for example body<List>(body)) in order to
invoke the ParameterizedTypeReference shortcut. If it is not specified,
the extension is shadowed by the Java body(Object) method which is not
able to use Kotlin type inference to resolve the generic type.