I have a Spring Boot Rest Controller, which receives a post body with a validated field. If I send a body that that violates the validation, I get an error response.
But in that response I am missing the detailed description of the errors, which part of the validation is failing. In the logged Exception in the Backend this is included, but I would like to have is visible for the client:
HTTP Response:
{
"timestamp": "2020-11-19T12:15:34.957+00:00",
"status": 400,
"error": "Bad Request",
"message": "Validation failed for object='postBody'. Error count: 1",
// <-- here I am missing the errors field that contains the (list of) error messages.
"path": "/comments"
}
Controller and PostBody:
@RestController
@Validated
public class CommentsController {
public void createComment(PostBody postBody) {
//do stuff
}
}
public class PostBody {
private String text;
@Size(max = 10)
public String getText() {
return text;
}
// setter
}
Exception in the backend logs contains the errors:
2020-11-19 13:15:34 WARN o.s.w.s.m.s.DefaultHandlerExceptionResolver -
Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in
public CommentsController.createComment(PostBody):
[Field error in object 'postBody' on field 'text': rejected value [my very long input];
codes [Size.postBody.text,Size.text,Size.java.lang.String,Size]; arguments
[org.springframework.context.support.DefaultMessageSourceResolvable: codes [postBody.text,text];
arguments []; default message [text],10,0]; default message [size must be between 0 and 10]] ]
Do I need to configure anything additional to get the errors details into the response? I would expect this to be included out of the box.