How to combine ResponseEntity with Flux or Mono For CRUD operations

眉间皱痕 提交于 2021-01-05 08:50:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!