你的方法
@ExceptionHandler(IllegalArgumentException.class)public @ResponseBody Map<String, Object> handleException(final Exception e, final HttpServletRequest request, Writer writer)
不起作用,因为它的返回类型错误。@ExceptionHandler方法只有两种有效的返回类型:
- 串
- ModelAndView。
有关更多信息,请参见http://static.springsource.org/spring/docs/3.0.x/spring-framework-
reference/html/mvc.html。这是链接中的特定文本:
返回类型可以是String,它被解释为视图名称或ModelAndView对象。
回应评论
Thanx,看来我太过分了。不好。有什么主意如何以xml / json格式自动提供异常?– Sven Haiges 7小时前
这是我所做的事情(我实际上是在Scala中完成的,因此我不确定语法是否完全正确,但是您应该明白要点)。
@ExceptionHandler(Throwable.class)@ResponseBodypublic void handleException(final Exception e, final HttpServletRequest request, Writer writer){ writer.write(String.format( "{"error":{"java.class":"%s", "message":"%s"}}", e.getClass(), e.getMessage()));}


