SpringBoot 全局異常錯誤頁面(使用繼承ErrorController方法)
1. 建立項目,依賴Web 和 thymeleaf
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2. 準備兩個異常錯誤頁面
在template中建立error文件夾,在error文件夾中建立4xx.html 和 5xx.htmlhtml
3.準備自定義錯誤處理類
com.zhl.springbootexceptionjunit.controller.MyBasicErrorControllerjava
- SpringBoot 出現異常時會查找 /error 視圖, 若是沒有則會根據錯誤碼查找對應 error/400.html 之類的錯誤靜態頁面
- 此處重寫ErrorController,實現/error 視圖,根據錯誤類型,跳轉至對應的視圖,因爲使用視圖技術,需引用模板引擎(本例用thymeleaf)
package com.zhl.springbootexceptionjunit.controller; import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.request.ServletWebRequest; import org.springframework.web.context.request.WebRequest; import javax.servlet.http.HttpServletRequest; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * 定製ErrorController,目的是能使SpringBoot找到本身定製的錯誤頁面 * 大部分的代碼BasicController一致,關鍵點是修改錯誤頁面的路徑 */ @Controller @RequestMapping(value = "/error") public class MyBasicErrorController implements ErrorController { @RequestMapping(produces = {"text/html"})//返回給瀏覽器 public String handlerError(HttpServletRequest request, Model model){ WebRequest webRequest = new ServletWebRequest(request);//對request進行包裝,目的是能操做更多的方法 HttpStatus status = this.getStatus(request);//獲取status String path = (String) webRequest.getAttribute("javax.servlet.error.request_uri", 0); String message = (String) webRequest.getAttribute("javax.servlet.error.message", 0); if(message.equals("")){ message = "No Available Message"; } //攜帶錯誤數據信息 model.addAttribute("timestamp", new Date()); model.addAttribute("statusCode", status.value()); model.addAttribute("error", status.getReasonPhrase()); model.addAttribute("message", message); model.addAttribute("path", path); int i = status.value() / 100;//判斷是4xx仍是5xx錯誤 if(i == 4){ return "error/4xx";//使用本身定製的錯誤頁面 }else if(i == 5){ return "error/5xx";//使用本身定製的錯誤頁面 } return null; } @RequestMapping//返回給客戶端 public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) { WebRequest webRequest = new ServletWebRequest(request);//對request進行包裝,目的是能操做更多的方法 HttpStatus status = this.getStatus(request);//獲取status Map<String, Object> map = new HashMap<>(); if (status == HttpStatus.NO_CONTENT) { return new ResponseEntity(status); } else { String path = (String) webRequest.getAttribute("javax.servlet.error.request_uri", 0); String message = (String) webRequest.getAttribute("javax.servlet.error.message", 0); map.put("timestamp", new Date()); map.put("statusCode", status.value()); map.put("error", status.getReasonPhrase()); map.put("message", message); map.put("path", path); return new ResponseEntity(map, status); } } protected HttpStatus getStatus(HttpServletRequest request) { Integer statusCode = (Integer)request.getAttribute("javax.servlet.error.status_code"); if (statusCode == null) { return HttpStatus.INTERNAL_SERVER_ERROR; } else { try { return HttpStatus.valueOf(statusCode); } catch (Exception var4) { return HttpStatus.INTERNAL_SERVER_ERROR; } } } @Override public String getErrorPath() { return "null"; } }
4.測試 代碼
com.zhl.springbootexceptionjunit.controller.UsersControllerweb
@Controller public class UsersController { @RequestMapping("showinfo") public String showinfo(){ String str=null; str.length(); return "ok"; } }
測試1,運行時錯誤:spring
測試2:無資源的狀況瀏覽器
參考: https://blog.csdn.net/qq_40634846/article/details/107710404springboot