Spring @ExceptionHandler und RedirectAttributes

Spring @ExceptionHandler und RedirectAttributes

Seit Spring 4.3.5 und 5.0 M4 unterstützt es das ArgumentRedirectAttributesin der Methode@ExceptionHandler.

    @ExceptionHandler(MyCustomException.class)
    public String handleError1(MyCustomException e, RedirectAttributes redirectAttributes) {

        redirectAttributes.addFlashAttribute("message", "abcdefg");
        return "redirect:/viewName";

    }

    @ExceptionHandler(MultipartException.class)
    public String handleError2(MultipartException e, RedirectAttributes redirectAttributes) {

        redirectAttributes.addFlashAttribute("message", e.getCause().getMessage());
        return "redirect:/viewName";

    }

P.S Read this SPR-14651

Noted
Wenn Sie Spring <4.3.5 verwenden, fügen Sie diesesRedirectAttributes nicht als Argument in die@ExceptionHandler-Methode ein, da Spring sonst das nicht abfangen kann Ausnahme ausgelöst.