Spring异常处理:不调用@exception处理程序

时间:2014-09-14 14:03:55

标签: java spring spring-mvc

林'最近使用冬眠,百里香和弹簧安全处理弹簧应用。

我偶然发现了一个情况:我做了一个自定义异常:

@ResponseStatus(HttpStatus.BAD_REQUEST)
public class GlobalUrlParametersErrorsException extends RuntimeException {

    private String errorMsgSignature;

    private String errorMsgContent;

    private String redirectionUrl;

    public String getErrorMsgSignature() {
        return errorMsgSignature;
    }

    public void setErrorMsgSignature(String errorMsgSignature) {
        this.errorMsgSignature = errorMsgSignature;
    }

    public String getErrorMsgContent() {
        return errorMsgContent;
    }

    public void setErrorMsgContent(String errorMsgContent) {
        this.errorMsgContent = errorMsgContent;
    }

    public String getRedirectionUrl() {
        return redirectionUrl;
    }

    public void setRedirectionUrl(String redirectionUrl) {
        this.redirectionUrl = redirectionUrl;
    }

    public GlobalUrlParametersErrorsException(String errorMsgSignature, String errorMsgContent, String redirectionUrl){

        this.errorMsgSignature = errorMsgSignature;
        this.errorMsgContent = errorMsgContent;
        this.redirectionUrl = redirectionUrl;
    }

}

我写的其他一些自定义异常扩展了这个例外。

为了处理这些异常,我添加了@ControllerAdvice

@ControllerAdvice
public class GlobalDefaultExceptionHandler {
//some other exception like hibernate exception ...

    @ExceptionHandler( value = GlobalUrlParametersErrorsException.class)//{InvalidDateException.class, InvalidRole.class, NoSuchUser.class, NoSuchNotification.class,NoSuchConsultation.class,NoSuchRdv.class})
    public ModelAndView whenUrlDateParametersSuckMethod(RedirectAttributes redirectAttributes,GlobalUrlParametersErrorsException e){

        redirectAttributes.addFlashAttribute(e.getErrorMsgSignature(),e.getErrorMsgContent());
        return new ModelAndView("redirect:"+e.getRedirectionUrl());
    }

    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        ModelAndView m = new ModelAndView("errors/defaultError");
        m.addObject("exception", e.toString());
        m.addObject("url", req.getRequestURL());
        return m;
    }

}

但我仍然没有抓住我在控制器中抛出的异常(我得到glassfish错误页面)

那我的approch有什么问题?

PS:当我删除我的自定义异常的异常处理程序,并且只让另一个异常处理程序时,它可以正常工作

2 个答案:

答案 0 :(得分:0)

尝试在弹簧配置中添加此行:

<mvc:annotation-driven/> 

如果你没有说出来,你的班级将无法加载。

答案 1 :(得分:0)

显然,您无法在@ExceptionHandler的方法参数中使用RedirectAttributes:处理程序被忽略。 目前the docs说:

  

与使用@RequestMapping注释注释的标准控制器方法非常相似,@ ExceptionHandler方法的方法参数和返回值可以是灵活的。例如,可以在Servlet环境中访问HttpServletRequest,在Portlet环境中访问PortletRequest。

看起来您可以使用@RequestMapping方法的相同参数,但它不能与RedirectAttributes一起使用。

解决方法是转发到@RequestMapping:

@ExceptionHandler(MyException.class)
public String handleMyException(MyException e) {
    return "forward:/internalError";
}

@RequestMapping("/internalError")
public String internalError(RedirectAttributes redirectAttributes) {
    // do stuff with redirectAttributes...
    return "redirect:/someplace";   
}