Your method
@ExceptionHandler(IllegalArgumentException.class)
public @ResponseBody Map<String, Object> handleException(final Exception e, final HttpServletRequest request, Writer writer)
does not work because it has the wrong return type. @ExceptionHandler methods have only two valid return types:
- String
- ModelAndView.
See http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html for more information. Here’s the specific text from the link:
The return type can be a String, which
is interpreted as a view name or a
ModelAndView object.
In response to the comment
Thanx, seems I overread this. That’s
bad… any ideas how to provides
exceptions automatically in xml/json
format? – Sven Haiges 7 hours ago
Here’s what I’ve done (I’ve actually done it in Scala so I’m not sure if the syntax is exactly correct, but you should get the gist).
@ExceptionHandler(Throwable.class)
@ResponseBody
public void handleException(final Exception e, final HttpServletRequest request,
Writer writer)
{
writer.write(String.format(
"{\"error\":{\"java.class\":\"%s\", \"message\":\"%s\"}}",
e.getClass(), e.getMessage()));
}