1. 程式人生 > 其它 >springboot異常處理

springboot異常處理

自定義錯誤頁面

現在這有個controller,裡邊有算數異常

@Controller
public class TestController {
    @RequestMapping("/test")
    public String test(){
        int a=3/0;
        return "ok";
    }

}

瀏覽器會這樣報錯(當出現錯誤時,程式自動尋找error介面,當找不到時,就只能用它自己的錯誤頁面)
在這裡插入圖片描述
加一個error.html,程式出錯時就會使用這個頁面進行報錯

<!DOCTYPE html>
<html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> ERROR </body> </html>

在這裡插入圖片描述

ExceptionHandler註解

使用這個註解必須返回ModelAndView,使用一個方法對異常進行處理,括號裡邊可以控制異常型別(多個)

@Controller
public class TestController {
@RequestMapping("/test") public String test(){ int a=3/0; return "ok"; } @ExceptionHandler(value={java.lang.ArithmeticException.class}) public ModelAndView nullPointerExceptionHandler(Exception e){ ModelAndView mv=new ModelAndView(); mv.
addObject("err",e.toString()); mv.setViewName("arithmeticError"); return mv; } }

thymeleaf

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
出錯了
<span th:text="${err}"></span>
${err}
</body>
</html>

[email protected]

僅僅在一個controller中使用HandlerException會造成冗餘,本方式是將處理放在全域性中,整個專案異常都能被捕捉

@Controller
public class TestController {
    @RequestMapping("/test")
    public String test(){
        int a=3/0;
        return "ok";
    }
    @RequestMapping("/test2")
    public String test2(){
        String str=null;
        str.length();
        return "ok";
    }

}

建立一個exception包,一個全域性異常類
在這裡插入圖片描述

@ControllerAdvice
public class GlobalException {
    @ExceptionHandler(value={java.lang.ArithmeticException.class})
    public ModelAndView arithmeticExceptionHandler(Exception e){
        ModelAndView mv=new ModelAndView();
        mv.addObject("err",e.toString());
        mv.setViewName("arithmeticError");
        return mv;
    }

    @ExceptionHandler(value={java.lang.NullPointerException.class})
    public ModelAndView nullPointerExceptionHandler(Exception e){
        ModelAndView mv=new ModelAndView();
        mv.addObject("err",e.toString());
        mv.setViewName("nullPointerError");
        return mv;
    }
}

SimpleMappingExceptionResolver

使用上邊這種方式對不同異常的處理會產生很多方法,本方式只使用一個方法就可以對不同異常實現跳轉
不過這種方法沒辦法傳遞異常物件資訊

@Controller
public class TestController {
    @RequestMapping("/test")
    public String test(){
        int a=3/0;
        return "ok";
    }
    @RequestMapping("/test2")
    public String test2(){
        String str=null;
        str.length();
        return "ok";
    }

}
@Configuration
public class GlobalException {
    @Bean
    public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver(){
        SimpleMappingExceptionResolver resolver=new SimpleMappingExceptionResolver();
        Properties  properties=new Properties();
        properties.put("java.lang.ArithmeticException","arithmeticError");//沒辦法傳遞異常資訊
        properties.put("java.lang.NullPointerException","nullPointerError");
        resolver.setExceptionMappings(properties);
        return  resolver;
    }


}

HandlerExceptionResolver

上邊那種方法沒辦法傳遞異常資訊,這種就解決了這個問題,感覺用這種方法就得

@Controller
public class TestController {
    @RequestMapping("/test")
    public String test(){
        int a=3/0;
        return "ok";
    }
    @RequestMapping("/test2")
    public String test2(){
        String str=null;
        str.length();
        return "ok";
    }

}

@Configuration
public class GlobalException implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler, Exception e) {
        ModelAndView mv=new ModelAndView();
        //判斷不同異常物件
        if(e instanceof NullPointerException){
            mv.setViewName("nullPointerError");
        }
        if(e instanceof ArithmeticException){
            mv.setViewName("arithmeticError");
        }
        mv.addObject("err",e.toString());
        return mv;
    }
}