thombergs / code-examples

A collection of code examples from blog posts etc.
MIT License
2.66k stars 2.64k forks source link

Improvment: OkOrNotFound controller base class method. #274

Open rcollette opened 1 year ago

rcollette commented 1 year ago

I think it would be a better example for your readers to have a controller base class method like:

private static final NOT_FOUND = ResponseEntity.notFound().build();

protected Mono<ResponseEntity<T>> OkOrNotFound(Mono<T> result){
  return result.map(ResponseEntity::ok).defaultIfEmpty(NOT_FOUND);
}

In use it's now just

return OkOrNotFound(result);