Add ResponseEntity.ofNullable() to deal with non-Optional nullable objects #29117
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
theme: kotlin
An issue related to Kotlin support
type: enhancement
A general enhancement
Milestone
Affects: All versions
Context
I like that
<T> ResponseEntity<T> ResponseEntity.of(Optional<T> body)
gives a 200 OK with a body if it is present inside the optional and a 404 for an empty Optional.Problem
However, sometimes you cannot or do not want to deal with
Optional
(for instance when using Kotlin) but still want to have a useful method like this.Current workaround
The current workaround would be to just wrap it inside an
Optional
:ResponseEntity.of(Optional.ofNullable(entity))
but this is pretty ugly and still creates an extra intermediateOptional
object.Proposal
My proposal would be to add a method:
<T> ResponseEntity<T> ResponseEntity.ofNullable(T body)
which can be used by both Java and Kotlin to turn a nullable object into a 200 OK with body or 404.The implementation would be pretty straightforward:
The text was updated successfully, but these errors were encountered: