Spring Bean Validation @Valid handling
Try this
@RequestMapping(value = "/valid")
public String validatedMethod(@Valid ValidObject object, BindingResult result) {
StringBuilder builder = new StringBuilder();
List<FieldError> errors = result.getFieldErrors();
for (FieldError error : errors ) {
builder.append(error.getField() + " : " + error.getDefaultMessage());
}
return builder.toString();
}
I believe a better way of doing this is using ExceptionHandler
.
In your Controller
you can write ExceptionHandler
to handle different exceptions. Below is the code for the same:
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ValidationFailureResponse validationError(MethodArgumentNotValidException ex) {
BindingResult result = ex.getBindingResult();
final List<FieldError> fieldErrors = result.getFieldErrors();
return new ValidationFailureResponse((FieldError[])(fieldErrors.toArray(new FieldError[fieldErrors.size()])));
}
When you send a bad request to the Controller, the validator throws an exception of type MethodArgumentNotValidException
. So the ideal way would be to write an exception handler to specifically handle this exception.
There you can create a beautiful response to tell the user of things which went wrong.
I advocate this, because you have to write this just once and many Controller
methods can use it. :)
UPDATE
When you use the @Valid annotation for a method argument in the Controller
, the validator is invoked automatically and it tries to validate the object, if the object is invalid, it throws MethodArgumentNotValidException
.
If Spring finds an ExceptionHandler
method for this exception it will execute the code inside this method.
You just need to make sure that the method above is present in your Controller.
Now there is another case when you have multiple Controller
s where you want to validate the method arguments. In this case I suggest you to create a ExceptionResolver
class and put this method there. Make your Controller
s extend this class and your job is done.