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.