Spring @ExceptionHandler и RedirectAttributes

Spring @ExceptionHandler и RedirectAttributes

Начиная с Spring 4.3.5 и 5.0 M4, он поддерживает аргументRedirectAttributes в методе@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
Если вы используете Spring <4.3.5, не добавляйте этотRedirectAttributes в качестве аргумента в метод@ExceptionHandler, иначе Spring не сможет поймать выброшенное исключение.

Рекомендации