How to do it...

After using Reactor Core specification to build the service layer, let us apply Mono<T> and Flux<T> streams to @Controllers by doing these steps:

  1. Open the ServiceController of the previous recipe again and add the following request handler showcasing the use of Reactor Stream operations:
@RequestMapping(value="/web/employeeNames.json",  
produces ="application/json", 
method = RequestMethod.GET,  
headers = {"Accept=text/xml, application/json"}) 
@ResponseBody 
public Callable<List<String>> jsonEmpNames(){ 
      Callable<List<String>> task =  
new Callable<List<String>>() { 
 
         @Override 
         public List<String> call () throws Exception { 
            List<String> names = new ArrayList<>(); 
            System.out.println("controller:jsonEmpNames  
               task executor: " +                                             Thread.currentThread().getName()); 
               Thread.sleep(5000); 
               employeeServiceImpl.readEmpFirstNames() 
 .subscribe((str)->{ 
                           names.add(str); 
                     }); 
               return names; 
             } 
         }; 
      return task; 
   } 
    
   @RequestMapping(value="/web/employeeFlux.json", 
produces ="application/json", 
method = RequestMethod.GET,  
headers = {"Accept=text/xml, application/json"}) 
   @ResponseBody 
   public Flux<Employee> jsonSoloEmployeeFlux(){ 
      return employeeServiceImpl.readEmployeesFlux(15); 
   } 
    
   @RequestMapping(value="/web/empLastnameFlux.json", 
produces ="application/json", 
method = RequestMethod.GET,  
headers = {"Accept=text/xml, application/json"}) 
   @ResponseBody 
   public Flux<Employee> jsonEmpLastNameFlux(){ 
      return  
         employeeServiceImpl.readEmployeesByAscLastName(); 
   } 
    
   @RequestMapping(value="/web/empAgeFlux.json", 
produces ="application/json", 
method = RequestMethod.GET,  
headers = {"Accept=text/xml, application/json"}) 
   @ResponseBody 
   public Flux<Employee> jsonEmpAgeFlux(){ 
      return employeeServiceImpl.readEmployeesByDescAge(); 
} 
  1. Save all files. The clean, build, and deploy. Run and test the request URLs in a browser.
..................Content has been hidden....................

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