Spring @ExceptionHandlerとRedirectAttributes

Spring @ExceptionHandlerおよびRedirectAttributes

Spring4.3.5および5.0M4以降、@ExceptionHandlerメソッドでRedirectAttributes引数をサポートします。

    @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はをキャッチできません。スローされた例外。