Spring3 @ExceptionHandler用于ServletRequestBindingException [英] Spring3 @ExceptionHandler for ServletRequestBindingException

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

问题描述

我正在使用一个默认的AnnotationMethodHandlerAdapter,我相信它应该启用对@ExceptionHandler的支持.不幸的是,如果对下面这样的处理程序方法的调用传入,则抛出ServletRequestBindingException,并且没有调用Exception处理程序.

I am using a Default AnnotationMethodHandlerAdapter which I believe should enable support for @ExceptionHandler. Unluckily, a ServletRequestBindingException is thrown if a call to a handler method like this below is coming in - and not Exception handler is invoked.

@RequestMapping(value = "/v1/products/{code}", method = RequestMethod.GET, headers = "Accept=application/xml,application/json")
@ResponseBody
public ProductDemoDTO getProductByCode(@PathVariable final String code,
        @RequestParam(required = false, defaultValue = "BASIC") final String options)
{
    //omitted
}

在ExceptionHandler中,从未调用过:

Here teh ExceptionHandler, never called:

@ExceptionHandler(Throwable.class)
@ResponseBody
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
//TODO not being called?
public void handleException(final Exception e, final HttpServletRequest request, final Writer writer) throws IOException
{
    writer.write(String.format("{\"error\":{\"java.class\":\"%s\", \"message\":\"%s\"}}", e.getClass(), e.getMessage()));
}

有人知道为什么不调用ExceptionHandler吗?

Does anyone know why the ExceptionHandler is not called?

推荐答案

您无法使用spring自定义实现来处理它.

you cannot handle it with spring custom implementation.

这可能不是一个很好的解决方案,但是您仍然可以使用web.xml <error-page>标记来捕获它.您可以从此处捕获异常类型或错误代码.

it may not an elegant solution but you still can catch it with web.xml <error-page> tag. you can catch exception type or error code from here.

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

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