Skip to content
Advertisement

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 ??

Advertisement

Answer

I was able to use map to log the results.

Mono<Employee> np = request.bodyToMono(Employee.class);
np.map(s->system.out.println(s));
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement