title: Spring 3.1.2 MVC @ExceptionHandler with @ResponseBody
body: <p>I'm trying to implement a Spring Web MVC @ExceptionHandler with a @ResponseBody to return an object containing validation errors.  (<a href="http://doanduyhai.wordpress.com/2012/05/06/spring-mvc-part-v-exception-handling/" rel="nofollow">Strategy E documented here</a>).</p>
  
  <p>In Spring 3.0.x, there was a confirmed <a href="https://jira.springsource.org/browse/SPR-6902" rel="nofollow">bug</a> , since resolved, that prohibited this from working properly.  I'm using Spring 3.1.2, and shouldn't be running into that one.</p>
  
  <p>I am, however, running into an exception"Could not find acceptable representation".  </p>
  
  <p>Here's the exception:</p>
  
  <pre><code>[10/21/12 12:56:53:296 CDT] 00000045 ExceptionHand E redacted.loggi
  ng.jdk14.LogWrapper error Failed to invoke @ExceptionHandler method: public 
  redacted.model.view.ValidationErrorResponse redacted.controller.Re
  stController.handleValidationException(redacted.util.ValidationExceptio
  n,javax.servlet.http.HttpServletResponse)
  
  Originating Class/Method:redacted.web.ui.filter.AccessControlFilter.pr
  ocessFilter()
  
                                   org.springframework.web.HttpMediaTypeNotAccepta
  bleException: Could not find acceptable representation
          at org.springframework.web.servlet.mvc.method.annotation.AbstractMessage
  ConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMeth
  odProcessor.java:115)
  </code></pre>
  
  <p>And here's the code:</p>
  
  <pre><code>@ExceptionHandler(ValidationException.class)
  @ResponseStatus(value = HttpStatus.PRECONDITION_FAILED)
  @ResponseBody
  public ValidationErrorResponse handleValidationException(ValidationException ex, HttpServletResponse response) {
      List&lt;ValidationError&gt; errs = new LinkedList&lt;ValidationError&gt;();
      for (ObjectError er : ex.getErrors()) {
          errs.add(new ValidationError(er.getObjectName(), er.getDefaultMessage()));
      }
  
      return new ValidationErrorResponse(errs);
  
  }
  
  @RequestMapping(value = "/search")
  @ResponseBody
  public SearchResult search(@Valid @ModelAttribute SearchRequest searchRequest, BindingResult bResult) {
      if (bResult.hasErrors()) {
          throw new ValidationException(bResult.getAllErrors());
      }
      return searchService.search(searchRequest);
  }
  </code></pre>
  
  <p>Any ideas?</p>