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";
        }
    
    }
    

     


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM