I'm trying to build a TODO application with Java Spring boot. Added a validation at the controller layer to make sure my todo name length is between 2 and 10 for the create API. I've configured to return the following response for the success:
{
"success": true,
"data": {"name": "hello", "completed": false},
"errors": []
}
I would like to return the same contract in failure case as well. But when I try to make a failing request (with name greater than 10 chars), the server is returning the following response somehow.
{
"timestamp": "2019-11-21T15:41:28.545+0000",
"status": 400,
"error": "Bad Request",
"errors": [
{
"codes": [
"Size.createTodoContract.name",
"Size.name",
"Size.java.lang.String",
"Size"
],
"arguments": [
{
"codes": [
"createTodoContract.name",
"name"
],
"arguments": null,
"defaultMessage": "name",
"code": "name"
},
10,
2
],
"defaultMessage": "size must be between 2 and 10",
"objectName": "createTodoContract",
"field": "name",
"rejectedValue": "attend wedding",
"bindingFailure": false,
"code": "Size"
}
],
"message": "Validation failed for object='createTodoContract'. Error count: 1",
"path": "/api/todos"
}
My payload validation is as follows:
@Getter
public class CreateTodoContract {
@Size(min = 2, max = 10)
private String name;
@NotNull
private boolean completed;
}
Please help me to understand where am I supposed to update the actual contract for failure cases?