Spring @ExceptionHandler 不适用于 @ResponseBody [英] Spring @ExceptionHandler does not work with @ResponseBody

查看:58
本文介绍了Spring @ExceptionHandler 不适用于 @ResponseBody的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试为休息控制器配置一个 spring 异常处理程序,该控制器能够根据传入的接受标头将映射渲染到 xml 和 json.它现在抛出一个 500 servlet 异常.

I try to configure a spring exception handler for a rest controller that is able to render a map to both xml and json based on the incoming accept header. It throws a 500 servlet exception right now.

这有效,它选择了 home.jsp:

This works, it picks up the home.jsp:

@ExceptionHandler(IllegalArgumentException.class)
public String handleException(final Exception e, final HttpServletRequest request, Writer writer)
{
    return "home";
}

这不起作用:

@ExceptionHandler(IllegalArgumentException.class)
public @ResponseBody Map<String, Object> handleException(final Exception e, final HttpServletRequest request, Writer writer)
{
    final Map<String, Object> map = new HashMap<String, Object>();
    map.put("errorCode", 1234);
    map.put("errorMessage", "Some error message");
    return map;
}

在同一个控制器中,通过相应的转换器将响应映射到 xml 或 json:

In the same controller mapping the response to xml or json via the respective converter works:

@RequestMapping(method = RequestMethod.GET, value = "/book/{id}", headers = "Accept=application/json,application/xml")
public @ResponseBody
Book getBook(@PathVariable final String id)
{
    logger.warn("id=" + id);
    return new Book("12345", new Date(), "Sven Haiges");
}

有人吗?

推荐答案

你的方法

@ExceptionHandler(IllegalArgumentException.class)
public @ResponseBody Map<String, Object> handleException(final Exception e, final HttpServletRequest request, Writer writer)

不起作用,因为它具有错误的返回类型.@ExceptionHandler 方法只有两种有效的返回类型:

does not work because it has the wrong return type. @ExceptionHandler methods have only two valid return types:

  • 字符串
  • ModelAndView.

参见 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html 了解更多信息.以下是链接中的具体文字:

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:

返回类型可以是String,即被解释为视图名称或ModelAndView 对象.

The return type can be a String, which is interpreted as a view name or a ModelAndView object.

回复评论

Thanx,我好像把这个看多了.那是不好...任何想法如何提供在 xml/json 中自动异常格式?– Sven Haiges 7 小时前

Thanx, seems I overread this. That's bad... any ideas how to provides exceptions automatically in xml/json format? – Sven Haiges 7 hours ago

这是我所做的(我实际上是在 Scala 中完成的,所以我不确定语法是否完全正确,但您应该明白要点).

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()));
}

这篇关于Spring @ExceptionHandler 不适用于 @ResponseBody的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆