简体   繁体   中英

Print body in handler from post request in spring webflux

I am completely new to Reactive Spring Webflux. I am writing a handler for a Post request which should

  1. Accept Json body(Employee id, name and role),
  2. Fetch some more Employee details from database 1 using id field,
  3. Return Employee json back with additional attributes like age and dept..

My router code is below:

 @Bean       
        -- something like this       
       POST("/empl/create").and(contentType(APPLICATION_JSON)), emplHandler::getMoreEmplDetails
  

Post Body:

   {
         "id":"213"
         "name": "John",
         "role": "Manager"
    }

Model Class looks like this

Public class Employee    
    {
       Public string  id;
       Public string name;
       Public string role;
       Public string dept;
       Public int age;
    }

Handler Code is Below

public Mono<ServerResponse>getMoreEmplDetails(ServerRequest request){
               Mono<Employee> np = request.bodyToMono(Employee.class);
               np.subscribe(x-> System.out.println("Print Body"+x)); 
                                                            //This returns a MonoOnErrorResume

                       /* More code should follow here*/

Return Mono.empty()// THIS IS TEMPORARY. I WANT TO RETURN COMPLETE EMPL JSON WITH AGE AND DEPT HERE
};

Problem is I want to print body for debugging purpose but Keep getting MonoOnErrorResume as indicated in comments in handler code. How do I make sure that my Body was received fine by the handler method??

I was able to use map to log the results.

Mono<Employee> np = request.bodyToMono(Employee.class);
np.map(s->system.out.println(s));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM