Skip to content

Conversation

@sdeleuze
Copy link
Contributor

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>(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.

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.
*/
public static <T> BodyInserter<T, ReactiveHttpOutputMessage> fromObject(T body) {
Assert.notNull(body, "'body' must not be null");
ReactiveAdapter adapter = ReactiveAdapterRegistry.getSharedInstance().getAdapter(body.getClass());
Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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.

@sdeleuze sdeleuze added this to the 5.2 RC1 milestone Jul 2, 2019
@sdeleuze sdeleuze closed this in 2b4d6ce Jul 7, 2019
rstoyanchev added a commit that referenced this pull request Aug 20, 2019
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
bclozel added a commit to spring-projects/spring-boot that referenced this pull request Aug 20, 2019
This commit adapts to recent changes in Spring Framework.
See spring-projects/spring-framework#23212
pull bot pushed a commit to scope-demo/spring-boot that referenced this pull request Aug 20, 2019
This commit adapts to recent changes in Spring Framework.
See spring-projects/spring-framework#23212
@sdeleuze sdeleuze deleted the body-object branch April 28, 2021 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants