In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.