Spring Bean Validation @Valid handling
Asked Answered
C

4

16

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 ConstraintViolations 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();
    }
}
Corwun answered 17/7, 2014 at 19:11 Comment(0)
O
32

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 Controllers 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 Controllers extend this class and your job is done.

Osithe answered 17/7, 2014 at 19:40 Comment(6)
Ok, this seems DRY. Could you elaborate a bit more? How do I wire this up to my validatedMethod? Oh, and the method is lacking a name.Corwun
ValidationFailureResponse seems to not be in any of my dependencies, and Google doesn't think it exists. Which Maven dependency could I add?Corwun
this is your custom response class. I said you can fabricate and send any response object you want :)Osithe
Let us continue this discussion in chat.Corwun
Referring to your update: You can now use @ControllerAdvice to define define global exception handlers.Sogdiana
Is there way I can produce the result of Set<ConstraintViolation<ValidObject>> constraintViolations = validator .validate(object); using @Valid in MethodArgumentNotValidExeption ?? I get only one field which is being voilated, but I want all of the fields which are voilated.Gracious
P
7

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();
}
Pyoid answered 17/7, 2014 at 19:30 Comment(1)
Cheers, works like a charm :) Would be nice though, not having to do this for every validated method.Corwun
G
3

When you use @Valid and doing bad request body Spring handle MethodArgumentNotValidException You must create special class and extend ResponseEntityExceptionHandler and override handleMethodArgumentNotValid Example

@ControllerAdvice
public class ControllerExceptionHandler extends ResponseEntityExceptionHandler {

  @ExceptionHandler(UserExistException.class)
  public ResponseEntity<Object> handleUserExistException(
        UserExistException e, WebRequest request) {

    Map<String, Object> body = new LinkedHashMap<>();
    body.put("timestamp", LocalDateTime.now());
    body.put("status", HttpStatus.BAD_REQUEST.value());
    body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase());
    body.put("message", e.getMessage());
    body.put("path", request.getDescription(false).replace("uri=", ""));


    return new ResponseEntity<>(body, HttpStatus.BAD_REQUEST);
  }

  @Override
  protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

    Map<String, Object> body = new LinkedHashMap<>();
    body.put("timestamp", LocalDateTime.now());
    body.put("status", HttpStatus.BAD_REQUEST.value());
    body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase());
    body.put("path", request.getDescription(false).replace("uri=", ""));
    return new ResponseEntity<>(body, headers, status);
  }
}
Gomez answered 22/7, 2022 at 6:32 Comment(1)
Yes, good addition. I use @ControllerAdvice for some time now, to avoid code duplication and to have all Exception based response handling in one place.Corwun
L
0

The answer by @dharam works. For users at Spring v4.3, Here's a nice implementation which uses a Custom Exception class to handle exception by type.

@RestControllerAdvice
public class CustomExceptionClass extends ResponseEntityExceptionHandler{
   @ExceptionHandler(value = MethodArgumentNotValidException.class)
   public ResponseEntity<Object> handleException(MethodArgumentNotValidException ex, WebRequest req){
   // Build your custom response object and access the exception message using ex.getMessage()
}
}

This method will enable handling all @Valid exceptions across all of your @Controller methods in a consolidated way

Longsome answered 20/10, 2021 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.