Spring @ExceptionHandler et RedirectAttributes

Spring @ExceptionHandler et RedirectAttributes

Depuis Spring 4.3.5 et 5.0 M4, il prend en charge l'argumentRedirectAttributes dans la méthode@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
Si vous utilisez Spring <4.3.5, n'ajoutez pas ceRedirectAttributes comme argument dans la méthode@ExceptionHandler, sinon, Spring ne pourra pas attraper le exception levée.