title: Spring Rest ErrorHandling @ControllerAdvice / @Valid
body: <p>I'm having trouble when Using <strong>@ControllerAdvice</strong> and <strong>@Valid</strong> annotations together in a REST controller.</p>
  
  <p>I have a rest controller declared as follows:</p>
  
  <pre><code>@Controller
  public class RestExample {
  
      ...
  
      /**
       * &lt;XmlRequestUser&gt;&lt;username&gt;user1&lt;/username&gt;&lt;password&gt;password&lt;/password&gt;&lt;name&gt;Name&lt;/name&gt;&lt;surname&gt;Surname&lt;/surname&gt;&lt;/XmlRequestUser&gt;
       * 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();
          user.setUsername(xmlUser.getUsername());
          user.setPassword(xmlUser.getPassword());
          user.setName(xmlUser.getName());
          user.setSurname(xmlUser.getSurname());
  
          // add user to the database
          StaticData.users.put(xmlUser.getUsername(), user);
          LOG.info("added user " + xmlUser.getUsername());
  
          return "added user " + user.getUsername();
      }
  }
  </code></pre>
  
  <p>And an ErrorHandler class:</p>
  
  <pre><code>@ControllerAdvice
  public class RestErrorHandler extends ResponseEntityExceptionHandler {
  
      private static Logger LOG = Logger.getLogger(RestErrorHandler.class);
  
  
      @ExceptionHandler(RuntimeException.class)
      public ResponseEntity&lt;Object&gt; handleException(final RuntimeException e, WebRequest request) {
          LOG.error(e);
  
          String bodyOfResponse = e.getMessage();
          return handleExceptionInternal(e, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request);
      }
  }
  </code></pre>
  
  <p>The problem is that, if I add a <em>"throw new RuntimeException"</em> within the method <strong>RestExample.add</strong> then the exception is correctly handled by <strong>RestErrorHandler</strong> class.</p>
  
  <p>However when curling a not valid request to the controller the <strong>RestErrorHandler</strong> doesn't catch the exception thrown by the validator and I receive a <strong>400 BadRequest</strong> response. (For not valid request I mean an xml request where Username is not specified)</p>
  
  <p>Note that <strong>XmlRequestUser</strong> class is autogenerated by the plugin <strong><a href="http://mojo.codehaus.org/jaxb2-maven-plugin/">maven-jaxb2-plugin</a> + <a href="https://github.com/krasa/krasa-jaxb-tools">krasa-jaxb-tools</a></strong> (pom.xml):</p>
  
  <pre><code>&lt;plugin&gt;
      &lt;groupId&gt;org.jvnet.jaxb2.maven2&lt;/groupId&gt;
      &lt;artifactId&gt;maven-jaxb2-plugin&lt;/artifactId&gt;
      &lt;executions&gt;
          &lt;execution&gt;
              &lt;goals&gt;
                  &lt;goal&gt;generate&lt;/goal&gt;
              &lt;/goals&gt;
          &lt;/execution&gt;
      &lt;/executions&gt;
      &lt;configuration&gt;
          &lt;schemaDirectory&gt;src/main/xsd&lt;/schemaDirectory&gt;
          &lt;schemaIncludes&gt;
              &lt;include&gt;*.xsd&lt;/include&gt;
          &lt;/schemaIncludes&gt;
          &lt;args&gt;
              &lt;arg&gt;-XJsr303Annotations&lt;/arg&gt;
              &lt;arg&gt;-XJsr303Annotations:targetNamespace=http://www.foo.com/bar&lt;/arg&gt;
          &lt;/args&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;com.github.krasa&lt;/groupId&gt;
                  &lt;artifactId&gt;krasa-jaxb-tools&lt;/artifactId&gt;
                  &lt;version&gt;${krasa-jaxb-tools.version}&lt;/version&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/configuration&gt;
  &lt;/plugin&gt;
  </code></pre>
  
  <p>The generated class has correctly a @NotNull Annotation on Username and Password fields.</p>
  
  <p>My <strong>context.xml</strong> is very easy, containing just a scanner for controllers and enabling mvc:annotation-driven</p>
  
  <pre><code>&lt;context:component-scan base-package="com.aa.rest" /&gt;
  &lt;mvc:annotation-driven /&gt;
  </code></pre>
  
  <p>Does anybody know how to make working @ControllerAdvice and @Valid annotations together in a REST controller?</p>
  
  <p>Thanks in advance.
  Antonio</p>