Saravana Kumar M
Saravana Kumar M

Reputation: 490

Handle Custom Validations and Exceptions in REST with SpringBoot

Sample Project Attached Here

I am following this article API Error Handling (also please suggest if there is a better way to do this) to handle exceptions and validations in a generic way in my spring boot rest service. (i am new to spring and rest, so i am going through different articles for my requirement)

Basic idea about the requirement:

(Need to validate the POST request and send the validation errors to the client in a structure way. There could be multiple validation errors)

Whenever i get a POST request from my client, i need to validate the RequestBody. So I added @Valid on the parameter and @NotNull on the properties which i want to validate. Upon receiving the POST request spring is validating the request and throwing MethodArgumentNotValidException which is fine since i have some mandatory field missing. I am handling it in a common place with @ControllerAdvice. After hitting the appropriate method handleMethodArgumentNotValid(...), i am constructing my custom error response APICustomError which i following from the above mentioned article.

When i have multiple validation errors, i am able to loop all the errors and adding it to a list and constructing the ResponseEntity with my custom error. But the returned ResponseEntity does not have my added validation errors.

i understood the article and implemented the same in my project but really didn't get what i am missing.

The below is the output said in the article and what i am expecting is:

{
   "apierror":{
      "status":"BAD_REQUEST",
      "timestamp":"10-07-2019 12:53:24",
      "message":"Validation error",
      "subErrors":[
         {
            "object":"person",
            "field":"id",
            "rejectedValue":null,
            "message":"ID cannot be null"
         },
         {
            "object":"person",
            "field":"name",
            "rejectedValue":null,
            "message":"name cannot be null"
         }
      ]
   }
}

but below is what i am getting. i don't see the subErrors part at all.

{"message":"Validation Error","debugMessage":null,"detail":null,"httpStatus":"BAD_REQUEST","timestamp":"2019-07-10T17:08:00.52"}

Any help is appreciated.

Upvotes: 1

Views: 2709

Answers (2)

Rajan Khurana
Rajan Khurana

Reputation: 1

Try this one In your controller advice

protected ResponseEntity<Object> handleMethodArgumentNotValid(
                MethodArgumentNotValidException ex,
                HttpHeaders headers, 
                HttpStatus status, WebRequest request) {
    Response response = new Response();
    List<FieldError> errors = ex.getBindingResult().getFieldErrors();
    List<SubError> errors = new ArrayList<>();

    for (FieldError e : errors) {
        SubError error = new SubError():
        error.setMessage(String.format(MESSAGE_FORMAT, egetCOde));
        errors.add(error);
    }
    response.setSubError(errors);
    return new ResponseEntity(response, HttpStatus.BAD_REQUEST);
}

Upvotes: 0

Emanuel Ramirez
Emanuel Ramirez

Reputation: 412

You need to add getter and setters in APICustomError for properly serialize your object. Also you need public constructor and getter/setters for inner class APIValidationError. I suggest you use Lombok.

After that you will see the errors, something like this...

{
   "message": "Validation Error",
   "debugMessage": null,
   "subErrors": [
       {
           "object": "personDTO",
           "field": "id",
           "rejectedValue": null,
           "validationErrorMessage": "ID cannot be null."
       }
   ],
   "detail": null,
   "httpStatus": "BAD_REQUEST",
   "timestamp": "2019-07-10T10:25:44.1705441"
}

Upvotes: 2

Related Questions