1

I am developing reactive-spring-boot web service and also using reactive-mongodb to store data. I want to return my customize response for API. example as below:

[
      {
          "result": [
        {
          "code": "",
          "data": "",
          "error": ""
        }
      ]
    }

Sample Method:

@PostMapping(value="/addEmployee")

  public Mono<Response> addEmployeeDetails(Employee employee){

    Response response = new Response();

     if(employee.getEmpID() == null){
          return response(getResponse());
     }

     Repository.save(employee);

     return response(getResponse());
}

As you see in the code I have response() method which builds the required response.But it mono gives default response as below: { "timestamp": "2018-09-29T16:23:21.287+0000", "status": 500, "error": "Internal Server Error", "message": "Internal Server Error", "path": "/employee" }

I am not able to set my own status code and error message.

cchantep
  • 9,118
  • 3
  • 30
  • 41
pratham
  • 73
  • 2
  • 7
  • stacktrace? Whats the response method doing? – Darren Forsythe Sep 29 '18 at 21:58
  • Hi Darren, Response is my POJO. – pratham Sep 30 '18 at 18:38
  • There's an un-handled error making it up past the the mvc call, without a stack trace and/or the application itself it's hard to tell what the root cause is :) – Darren Forsythe Oct 01 '18 at 07:07
  • I am writing a reactive web services. In one of the API I have added checks using if statement as given in code. eg: if(employee.getEmpID() == null){ return response(getResponse()); } How can I return customize error message here ? I get default error message as: { "timestamp": "2018-09-29T16:23:21.287+0000", "status": 500, "error": "Internal Server Error", "message": "Internal Server Error", "path": "/employee" } But I want to print: [ { "result": [ { "code": "", "data": "", "error": "" } ] – pratham Oct 01 '18 at 10:25
  • Ah, got you. You need to provide onError within the reactive chain, https://stackoverflow.com/questions/51024279/how-to-handle-errors-in-spring-reactor-mono-or-flux – Darren Forsythe Oct 01 '18 at 11:00
  • Actually I am want to do data validation before saving the data in MongoDB. So whatever data I get in ServiceRequest or request body. I need to do validation for it and is the data is not proper then throw error. So if you check my if condition I need to send a user response (customize response) using Mono. – pratham Oct 04 '18 at 10:00

0 Answers0