We see a need to improve ConstraintViolationException responses, as field
values could be less cryptic for REST api client than they are now.
JaxRS endpoint example:
@POST
@Path("/resource/{param}")
public void endpoint(
@Valid @PathParam("param") @Length(min = 100, max = 101) String pathParam,
@Valid @QueryParam("param") @Length(min = 200, max = 201) String queryParam,
@Valid @HeaderParam("param") @Length(min = 300, max = 301) String headerParam,
@Valid RequestBody body) {
}
static class RequestBody {
@Length(min = 6, max = 10)
String param = "";
@Valid
List<Item> list = Collections.singletonList(new Item());
static class Item {
@Length(min = 7, max = 10)
String param = "";
}
@Valid
NestedObject nested = new NestedObject();
static class NestedObject {
@Length(min = 8, max = 10)
String param = "";
}
}
Current output that needs improvements (empty field
for path, query and header param violations, lack of index for body list.item
)
{
"status": 400,
"title": "Bad Request",
"instance": "/resource/1",
"violations": [
{
"field": "",
"message": "length must be between 100 and 101"
},
{
"field": "",
"message": "length must be between 200 and 201"
},
{
"field": "",
"message": "length must be between 300 and 301"
},
{
"field": "param",
"message": "length must be between 6 and 10"
},
{
"field": "list.param",
"message": "length must be between 7 and 10"
},
{
"field": "nested.param",
"message": "length must be between 8 and 10"
}
]
}
Expected output (mind better field
values and new property in
in violation item):
{
"status": 400,
"title": "Bad Request",
"instance": "/resource/1",
"violations": [
{
"field": "param",
"in": "path",
"message": "length must be between 100 and 101"
},
{
"field": "param",
"in": "query",
"message": "length must be between 200 and 201"
},
{
"field": "param",
"in": "header",
"message": "length must be between 300 and 301"
},
{
"field": "param",
"in": "body",
"message": "length must be between 6 and 10"
},
{
"field": "list[0].param",
"in": "body",
"message": "length must be between 7 and 10"
},
{
"field": "nested.param",
"in": "body",
"message": "length must be between 8 and 10"
}
]
}
To keep this description short @FormParam was not included, as it is mutually exclusive with json body payload, but it should be properly handled as well.
field
values should also work properly if custom propertyNodeNameProvider
is configured for Hibernate Validator.