You can put your mapping in @ExceptionHandler
s that handle the MethodArgumentNotValidException
and the ConstraintViolationException
. Here's an example with the former.
Assume an object Foo
:
import javax.validation.constraints.Min;
public class Foo {
private int bar;
@Min(0)
public int getBar() {
return bar;
}
public void setBar(int bar) {
this.bar = bar;
}
}
bar
needs to be non-negative. Here's the controller.
import internal.sandbox.domain.Foo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
@Controller
public class SomeController {
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public Map<String,String> handleValidationFailure(MethodArgumentNotValidException e) {
Map<String, String> errors = new HashMap<>();
for (FieldError fieldError : e.getBindingResult().getFieldErrors()) {
errors.put(fieldError.getObjectName() + fieldError.getField(),
fieldError.getDefaultMessage());
}
return errors;
}
@RequestMapping(value = "/", method = RequestMethod.POST, consumes = "application/json")
public ResponseEntity<?> post(@Valid @RequestBody Foo foo) {
return new ResponseEntity<>(HttpStatus.OK);
}
}
Sending a POST with data { "bar":"-1" }
throws a MethodArgumentNotValidException
which is handled by the @ExceptionHandler
, resulting in a 404 with the following response body:
{"foobar":"must be greater than or equal to 0"}
I'm assuming you have a much smarter @ExceptionHandler
that can map your stuff better, but the exception has everything you need in it, as would the Set<ConstraintViolations>
field in the ConstraintViolationException
. At this point, your AngularJS service can check the rejection of the $http
promise that made the POST, and have a field day processing the error information.
You can write a similar @ExceptionHandler
to process ConstraintViolationException
s thrown by Hibernate.