问题
Hi i am trying to create a CRUD controller with repository but i couldn't find any example for using as reference. I want to do something like this below.
@RequestMapping(value = "/api/open/books", method = RequestMethod.GET)
public ResponseEntity<Flux<Book>> getBooks() {
Flux<Book> books = bookRepository.findAll();
// if books is null
return new ResponseEntity<Flux<Book>>(books, HttpStatus.NOT_FOUND);
// else
return new ResponseEntity<Flux<Book>>(books, HttpStatus.OK);
}
@RequestMapping(value = "/api/open/books", method = RequestMethod.PUT)
public ResponseEntity<Mono<Book>> updateBook(@RequestBody Book bookObject) {
Mono<Book> book = bookRepository.findById(bookObject.getId());
// if book is null
return new ResponseEntity<Flux<Book>>(books, HttpStatus.NOT_FOUND);
// else
bookRepository.save(book);
return new ResponseEntity<Flux<Book>>(books, HttpStatus.OK);
}
I have tried something like this but it is always returning OK because Flux or Mono object itself isn't null but it's data is null i couldn't find how to check it.
Flux<Book> books = bookRepository.findAll();
HttpStatus status = books != null ? HttpStatus.OK : HttpStatus.NOT_FOUND;
来源:https://stackoverflow.com/questions/65059178/how-to-combine-responseentity-with-flux-or-mono-for-crud-operations