Background
I have an error message class:
@XmlRootElement
public class ErrorMessage {
private String message;
public ErrorMessage() {
}
public ErrorMessage(String message) {
this.message = message;
}
public String getError() {
return message;
}
public void setError(String message) {
this.message = message;
}
}
This class has been assigned as a return value to an @ExceptionHandler in my Spring MVC REST controller:
@ExceptionHandler
@ResponseStatus(HttpStatus.NOT_FOUND)
@ResponseBody
ErrorMessage handleException(RuntimeException e) {
return new ErrorMessage("something went wrong");
}
Whenever the client triggers a RuntimeException after issuing a request with application/json as a Accept header, it receives a response with the correct status code and a matching JSON body:
{"error":"something went wrong"}
Alternatively, an XML body is received if the Accept header is application/xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<errorMessage><error>something went wrong</error></errorMessage>
Problem
Now, I would like to generify this solution by implementing a HandlerExceptionResolver instead so I don’t have to copy / paste the @ExceptionHandler to every controller (or create a common "parent controller" that the other controllers can extend).
However, the AbstractHandlerExceptionResolver.doResolveException() method returns a ModelAndView and not my propriety ErrorMessage, so I tried the following:
public class RuntimeExceptionHandlerExceptionResolver extends AbstractHandlerExceptionResolver {
@Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
if (ex instanceof RuntimeException) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
ModelAndView mav = new ModelAndView();
mav.addObject("error", "something went wrong");
return mav;
}
return null;
}
}
When debugging, I can see that the mav.addObject() method is called. The response on the client side has the expected status code, but the content type is text/html with html in the body, rather than the JSON or XLM content that was specified by the Accept header in the original request.
(Side note, the actual exception, response code and text message in the example above is not important, they just serve as simple example.)
Spring version: 3.1.1.RELEASE
I have spent some time investigating the matter and I have written a blog post in which I present a solution to the problem.
Update: If you are using Spring 3.2, you can take advantage of the @ControllerAdvice annotation. More details can be found in my second blog post.