當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot 全局异常处理
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot 全局异常处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
下面首先來觀察一個程序代碼,例如:現在建立一個控制器,而后這個控制器自己拋出一個異常。@Controller
public class MemberController extends AbstractBaseController{@RequestMapping(value="/get")@ResponseBodypublic String get() {System.out.println("除法計算:" + (10 / 0));return "hello world" ;} }
如果此時配置有錯誤頁,那么這個時候錯誤會統一跳轉到 500 所在的路徑上進行錯誤的顯示,但是如果說
現在希望能夠顯示 出錯誤更加詳細的內容呢?//@Configuration
public class ErrorPageConfig {
// @Beanpublic EmbeddedServletContainerCustomizer containerCustomizer() {return new EmbeddedServletContainerCustomizer() {@Overridepublic void customize(ConfigurableEmbeddedServletContainer container) {ErrorPage errorPage400 = new ErrorPage(HttpStatus.BAD_REQUEST,"/error_400.html");ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND,"/error_404.html");ErrorPage errorPage500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error_500.html");container.addErrorPages(errorPage400, errorPage404,errorPage500);}};}
}
所以這個時候可以單獨定義一個頁面進行錯誤的信息顯示處理,而這個頁面,可以定義在“src/main/view/templates/error.html”, 這個頁面里面要求可以輸出一些信息;1、 定義一個全局的異常處理類:package com.microboot.advice;import javax.servlet.http.HttpServletRequest;import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;@ControllerAdvice // 作為一個控制層的切面處理
public class GlobalExceptionHandler {public static final String DEFAULT_ERROR_VIEW = "error"; // 定義錯誤顯示頁,error.html@ExceptionHandler(Exception.class) // 所有的異常都是Exception子類public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) {
// 出現異常之后會跳轉到此方法ModelAndView mav = new ModelAndView(DEFAULT_ERROR_VIEW); // 設置跳轉路徑mav.addObject("exception", e); // 將異常對象傳遞過去mav.addObject("url", request.getRequestURL()); // 獲得請求的路徑return mav;}
}
2、 定義 error.html 頁面:<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot模版渲染</title>
<link rel="icon" type="image/x-icon" href="/images/study.ico"/>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<p th:text="${url}"/>
<p th:text="${exception.message}"/>
</body>
</html>
對于全局異常信息顯示除了采用以上的跳轉處理方式之外,也可以做的簡單一些,使用 Rest 進行顯示。范例:修改全局異常處理類package com.microboot.advice;import javax.servlet.http.HttpServletRequest;import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;//@ControllerAdvice// 作為一個控制層的切面處理
@RestControllerAdvice
public class GlobalExceptionHandler {public static final String DEFAULT_ERROR_VIEW = "error"; // 定義錯誤顯示頁,error.html@ExceptionHandler(Exception.class) // 所有的異常都是Exception子類public Object defaultErrorHandler(HttpServletRequest request,Exception e) {class ErrorInfo {private Integer code ;private String message ;private String url ;public Integer getCode() {return code;}public void setCode(Integer code) {this.code = code;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}}ErrorInfo info = new ErrorInfo() ;info.setCode(100); // 標記一個錯誤信息類型info.setMessage(e.getMessage());info.setUrl(request.getRequestURL().toString());return info ;}
// public ModelAndView defaultErrorHandler(HttpServletRequest request,
// Exception e) { // 出現異常之后會跳轉到此方法
// ModelAndView mav = new ModelAndView(DEFAULT_ERROR_VIEW); // 設置跳轉路徑
// mav.addObject("exception", e); // 將異常對象傳遞過去
// mav.addObject("url", request.getRequestURL()); // 獲得請求的路徑
// return mav;
// }
}
如果現在要想把異常的信息顯示的更加華麗一些(不是面對所有用戶),那么最好的做法就是使用全局異常處理
的方式完成。
?
總結
以上是生活随笔為你收集整理的SpringBoot 全局异常处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot 配置错误页
- 下一篇: SpringBoot 配置Tomcat运