SpringBoot-统一异常处理(404 ,500)

  1. 404
    /**
     * 描述:统一处理404异常
     *
     * @outhor ios
     * @create 2019-01-04 3:49 PM
     */
    @Controller
    public class NotFoundException implements ErrorController {
    
        @Override
        public String getErrorPath() {
            return "/error";
        }
    
        @RequestMapping("/error")
        public String error(ModelMap map){
            map.addAttribute("message", "404 not found link!");
            return "uploadStatus";
        }
    
    }
    
  2. 500
    /**
     * 描述: 统一处理服务器500异常
     *
     * @outhor ios
     * @create 2019-01-04 3:01 PM
     */
    @ControllerAdvice
    public class GloadExceptionController {
    
        @ExceptionHandler(Exception.class)
        public String exceptionHandler(Exception e, RedirectAttributes attributes){
            System.out.println(e.getClass().getName());
            attributes.addFlashAttribute("message", "error : " + e.getCause().getMessage());
            return "redirect:/uploadStatus";
        }
    
    }
    
原文地址:https://www.cnblogs.com/king-peng/p/10220365.html