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