I have created a Spring MVC REST service using Bean Validation 1.2 with the following method:
@RequestMapping(value = "/valid")
public String validatedMethod(@Valid ValidObject object) {
}
If object isn't valid, Tomcat informs me that The request sent by the client was syntactically incorrect.
and my validatedMethod
is never called.
How can I get the message that was defined in the ValidObject
bean? Should I use some filter or interceptor?
I know that I can rewrite like below, to get the set of ConstraintViolation
s from the injected Validator
, but the above seems more neat...
@RequestMapping(value = "/valid")
public String validatedMethod(ValidObject object) {
Set<ConstraintViolation<ValidObject>> constraintViolations = validator
.validate(object);
if (constraintViolations.isEmpty()) {
return "valid";
} else {
final StringBuilder message = new StringBuilder();
constraintViolations.forEach((action) -> {
message.append(action.getPropertyPath());
message.append(": ");
message.append(action.getMessage());
});
return message.toString();
}
}
validatedMethod
? Oh, and the method is lacking a name. – Corwun