Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use RxJava to build responsive REST API in Spring Boot

Shulou Source: shulou.com Published: 2022-06-02 06:01:18 10月04日 Update

In this issue, the editor will bring you about how to use RxJava to build responsive REST API in Spring Boot. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Responsive REST API

Build a simple CRUD responsive REST API that contains only authors and books. These are endpoints:

[POST] / api/authors → add author

[POST] / api/books → add Books

[PUT] / api/books/ {bookId} → updates book information according to book id

[GET] / api/books?limit= {limit} & page= {page} → pagination to get a list of books

[GET] / api/book/ {bookId} → gets book details based on book id

[DELETE] / api/book/ {bookId} → delete books

3. Dependence

Open pom.xml and add the following dependencies.

Org.springframework.boot spring-boot-starter-web 2.1.5.RELEASE org.springframework.boot spring-boot-starter-data-jpa 2.1.5.RELEASE io.reactivex rxjava 1.3.8 Io.reactivex rxjava-reactive-streams 1.2.1 com.h3database h3 1.4.199 org.projectlombok lombok true 1.18.8 org.springframework.boot spring-boot- Starter-test 2.1.5.RELEASE test org.mockito mockito-core 2.25.0 test

Note: keep in mind that you must add lines 19-23 dependencies. If you don't add this dependency, you'll get HttpMediaNotAcceptableException every time you click on responsive API. As you can see, I also added mockito as a dependency on the mock object in the unit test. But I will discuss unit testing in another article.

4. Service layer

For the service layer, the return values are not just regular data types, but I encapsulate them in RxJava's Single (single) data type. For example, the following code handles the addition of new books.

@ Override public Single addBook (AddBookRequest addBookRequest) {return saveBookToRepository (addBookRequest);} private Single saveBookToRepository (AddBookRequest addBookRequest) {return Single.create (singleSubscriber-> {Optional optionalAuthor = authorRepository.findById (addBookRequest.getAuthorId ()); if (! optionalAuthor.isPresent ()) singleSubscriber.onError (new EntityNotFoundException ()) Else {String addedBookId = bookRepository.save (toBook (addBookRequest)) .getId (); singleSubscriber.onSuccess (addedBookId);}} private Book toBook (AddBookRequest addBookRequest) {Book book = new Book (); BeanUtils.copyProperties (addBookRequest, book); book.setId (UUID.randomUUID (). ToString ()) Book.setAuthor (Author.builder () .id (addBookRequest.getAuthorId ()) .build ()); return book;}

As you can see, the return value of the addBook method is a string wrapped in RxJava.

5.web layer @ PostMapping (consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public Single addBook (@ RequestBody AddBookWebRequest addBookWebRequest) {return bookService.addBook (toAddBookRequest (addBookWebRequest)) .subscrib eOn (Schedulers.io ()) .map (s-> ResponseEntity.created (URI.create ("/ api/books/" + s)) .body (BaseWebResponse.successNoData () } private AddBookRequest toAddBookRequest (AddBookWebRequest addBookWebRequest) {AddBookRequest addBookRequest = new AddBookRequest (); BeanUtils.copyProperties (addBookWebRequest, addBookRequest); return addBookRequest;} this is how to use RxJava to build a responsive REST API in the Spring Boot shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Books authors information content units data articles types analysis encapsulation services testing not only professional small and medium code rich content notes characters strings Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple vpn Huawei Microsoft OPPO Reno