The reactive web application

Let's develop a RESTful web service to understand reactive implementation as stated here:

  1. Create Ch11_Spring_Reactive_Web as a dynamic web application.
  1. Add the JARs as shown in the following screenshot:
  1. Add Book.java from our previous project in the com.packt.ch03.beans package.
  2. Add the Front Controller mapping in web.xml as we did for Spring MVC and Spring RESTful applications.
  3. Add books-servlet.xml for adding the beans and package scanning, which allows the container to discover the Controllers in it. You can copy it from any of our previous Spring MVC applications.
  4. Add MyBookController in the com.packt.ch11.controllers package that will be used as a REST controller.
  5. Annotate the class by @RestController.
  1. Let's add the following method as a service to be fetched by the user:
@GetMapping("/books/{ISBN}") 
  public Mono<Book> getBook(@PathVariable long ISBN) { 
    Book book=new Book(); 
    book.setISBN(ISBN); 
    return Mono.justOrEmpty(book).subscribe(); 
} 

We have added Mono<T> in the response. The response serializes the object and forwards it. As the return type, once the Mono emits a value, the T is serialized asynchronously and sent back to the client. We can combine both approaches by augmenting the Mono request and returning that augmented chain as the resulting Mono, as we did in the preceding code.

  1. Launch the application and request for it from the browser, as follows:

http://localhost:8080/Ch11_Spring_Reactive_Web/books/100

You will get the details of the book displayed on the browser. You can even request to the service from the Postman, as we did in the RESTful application, to test them.

  1. We can even write the client using RestTemplate, as we did in the application developed earlier in the RESTful application. Create Main_Get_Book as a class with a main function, as shown in the following code:
Public class Main_Get_Book { 
  Public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    RestTemplate template=new RestTemplate(); 
    Book book=template.getForObject  
    ("http://localhost:8080/Ch11_Spring_Reactive_Web/books/14", 
    Book.class); 
    System.out.println(book.getAuthor()+"	"+book.getISBN()); 
  } 
} 
  1. Let's add one more method to get all the books in the controller:
@GetMapping("/books") 
public Flux<List<Book>> getAllBooks() { 
  ArrayList<Book>books=new ArrayList<Book>(); 
  books.add(new Book("Spring 5.0",1234l,"Packt Pub Publication",
500,"Learn Spring", "T.M.Jog")); books.add(new Book("Learn Modular Java",1234l,
"Packt Pub Publication",500,"Learn java", "Author1")); return Flux.just(books);
}
  1. Here, we used Flux<T> in the streaming scenarios in the input streaming when used as @RequestBody, and we have used it as the return type.
  2. To test the method, launch the application and use Postman by entering the URL as http://localhost:8080/Ch11_Spring_Reactive_Web/books and selecting the GET method.
  3. The REST client can be written as follows:
public class Main_Get_Book { 
    public static void main(String[] args) { 
      // TODO Auto-generated method stub 
 
      RestTemplate template = new RestTemplate(); 
      Book book = template.getForObject("http://localhost:8080/
Ch11_Spring_Reactive_Web/books/14", Book.class); System.out.println(book.getAuthor() + " " +
book.getISBN()); HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(
MediaType.APPLICATION_JSON)); HttpEntity<String> entity = new
HttpEntity<String>(headers);
ResponseEntity<List> responseEntity =
template.getForEntity(
"http://localhost:8080/Ch11_Spring_Reactive_Web/books",
List.class); List<ArrayList<Book>> books = responseEntity.getBody(); int i = 0; for (ArrayList l : books) { for (int j = 0; i < l.size(); j++) { LinkedHashMap<String, Book> map =
(LinkedHashMap<String, Book>) l.get(i); Set<Entry<String, Book>> set = map.entrySet(); System.out.println("*** Book:-"+i +" ****"); for (Entry<String, Book> entry : set) { System.out.print(entry.getValue() + " "); } i++; System.out.println(); } } } }
  1. We have already discussed RestTemplate in Chapter 9, Explore the Power of Restful Web Services, so let's just execute it and get the details on console. You can even use Postman to test the application.
  2. RestTemplate was known to us; however, the RestTemplate blocks the calls, and we are talking about non-blocking application development. As our application is non-blocking, we need a non-blocking client as well. Spring 5 has given us the WebClient solution.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.144.229