Performing a redirect from a spring MVC @ExceptionHandler method

后端 未结 4 818
心在旅途
心在旅途 2021-02-07 10:49

I want to have the following method:

@ExceptionHandler(MyRuntimeException.class)
public String myRuntimeException(MyRuntimeException e, RedirectAttributes redire         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-07 11:12

    You could always forward then redirect (or redirect twice).. First to another request mapping where you have normal access to RedirectAttributes, then again to your final destination.

        @ExceptionHandler(Exception.class)
        public String handleException(final Exception e) {
    
            return "forward:/n/error";
        }
    
        @RequestMapping(value = "/n/error", method = RequestMethod.GET)
        public String error(final RedirectAttributes redirectAttributes) {
    
            redirectAttributes.addAttribute("foo", "baz");
            return "redirect:/final-destination";
        }
    

提交回复
热议问题