error-handling java rest spring

Spring Rest ErrorHandling @ControllerAdvice / @Valid

I’m having trouble when Using @ControllerAdvice and @Valid annotations together in a REST controller.

I have a rest controller declared as follows:

public class RestExample {
* <XmlRequestUser><username>user1</username><password>password</password><name>Name</name><surname>Surname</surname></XmlRequestUser>
* curl -d "@restAddRequest.xml" -H "Content-Type:text/xml" http://localhost:8080/SpringExamples/servlets/rest/add
@RequestMapping(value="rest/add", method=RequestMethod.POST)
public @ResponseBody String add(@Valid @RequestBody XmlRequestUser xmlUser) {
User user = new User();
// add user to the database
StaticData.users.put(xmlUser.getUsername(), user);"added user " + xmlUser.getUsername());
return "added user " + user.getUsername();

And an ErrorHandler class:

public class RestErrorHandler extends ResponseEntityExceptionHandler {
private static Logger LOG = Logger.getLogger(RestErrorHandler.class);
public ResponseEntity<Object> handleException(final RuntimeException e, WebRequest request) {
String bodyOfResponse = e.getMessage();
return handleExceptionInternal(e, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request);

The problem is that, if I add a “throw new RuntimeException” within the method RestExample.add then the exception is correctly handled by RestErrorHandler class.

However when curling a not valid request to the controller the RestErrorHandler doesn’t catch the exception thrown by the validator and I receive a 400 BadRequest response. (For not valid request I mean an xml request where Username is not specified)

Note that XmlRequestUser class is autogenerated by the plugin maven-jaxb2-plugin + krasa-jaxb-tools (pom.xml):


The generated class has correctly a @NotNull Annotation on Username and Password fields.

My context.xml is very easy, containing just a scanner for controllers and enabling mvc:annotation-driven

<context:component-scan base-package="" />
<mvc:annotation-driven />

Does anybody know how to make working @ControllerAdvice and @Valid annotations together in a REST controller?

Thanks in advance.

You are on the right track, but you need to override the handleMethodArgumentNotValid() instead of the handleException() method, e.g.

public class RestErrorHandler extends ResponseEntityExceptionHandler {
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException exception,
HttpHeaders headers,
HttpStatus status,
WebRequest request) {
String bodyOfResponse = exception.getMessage();
return new ResponseEntity(errorMessage, headers, status);

From the JavaDoc of MethodArgumentNotValidException:

Exception to be thrown when validation on an argument annotated with @Valid fails.

In other words, a MethodArgumentNotValidException is thrown when the validation fails. It is handled by the handleMethodArgumentNotValid() method provided by the ResponseEntityExceptionHandler, that needs to be overridden if you would like a custom implementation.