Add body(Object) method to ServerResponse.BodyBuilder [SPR-15461] #20021
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
type: enhancement
A general enhancement
Milestone
Arjen Poutsma opened SPR-15461 and commented
We should add a
ServerResponse.BodyBuilder.body(Object)
method, to make it easier for users to set the response body to a non-Publisher type. Currently, this task can be accomplished viabody(BodyInserters.fromObject(Object))
, but because this method requires a static import ofBodyInserters
, the discoverability of it is not as high asbody(Publisher, Class))
. As such, users will go this route:instead of the more elegant:
However, adding the method has a serious consequence: there is the potential for accidental method overloading between a the new
Object
method and the existingPublisher
body
method. As a result, users will accidentally write a response with the publisher itself, rather than its contents:Of course, we cannot automatically call the correct method, because of the lacking
Class
argument. There are, however, a couple of other ways we can resolve this.bodyFromObject
. If we go this route, we should be consistent and also rename the existingPublisher
-basedbody
method tobodyFromPublisher
. As such, it has serious consequences for the brevity and UX of the API.Object
passed to body is aPublisher
, and throw an exception if so, warning the user of the error of their ways.Issue Links:
Referenced from: commits 30f61e0
The text was updated successfully, but these errors were encountered: