I'm trying to implement a Spring Web MVC @ExceptionHandler with a @ResponseBody to return an object containing validation errors. (Strategy E documented here).
In Spring 3.0.x, there was a confirmed bug , since resolved, that prohibited this from working properly. I'm using Spring 3.1.2, and shouldn't be running into that one.
I am, however, running into an exception"Could not find acceptable representation".
Here's the exception:
[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) And here's the code:
@ExceptionHandler(ValidationException.class) @ResponseStatus(value = HttpStatus.PRECONDITION_FAILED) @ResponseBody public ValidationErrorResponse handleValidationException(ValidationException ex, HttpServletResponse response) { List<ValidationError> errs = new LinkedList<ValidationError>(); 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); } Any ideas?