一、spring1.x中處理方式
@Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return new EmbeddedServletContainerCustomizer() { @Override public void customize(ConfigurableEmbeddedServletContainer container) { ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401"); ErrorPage error405Page = new ErrorPage(HttpStatus.METHOD_NOT_ALLOWED, "/405"); ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404"); ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500"); container.addErrorPages(error401Page,error405Page, error404Page, error500Page); } }; }
二、spring2.x中處理方式
@Component public class ErrorConfig implements ErrorPageRegistrar { @Override public void registerErrorPages(ErrorPageRegistry registry) { ErrorPage error400Page = new ErrorPage(HttpStatus.BAD_REQUEST, "/error400Page"); ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/error401Page"); ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/error404Page"); ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error500Page"); registry.addErrorPages(error400Page,error401Page,error404Page,error500Page); } }
那么此時只要出現了錯誤,就會找到相應的 http 狀態碼,而后跳轉到指定的錯誤路徑上進行顯示。
三、ErrorPageAction跳轉處理
@Controller public class ErrorPageAction { @RequestMapping(value = "/error400Page") public String error400Page() { return "404"; } @RequestMapping(value = "/error401Page") public String error401Page() { return "401"; } @RequestMapping(value = "/error404Page") public String error404Page(Model model) { model.addAttribute("code","6666666"); model.addAttribute("msg","服務器降級中......"); return "404"; } @RequestMapping(value = "/error500Page") public String error500Page(Model model) { return "500"; } }
404.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> </head> <body> <p th:text="${code}"/> <p th:text="${msg}"/> </body> </html>
四、全局controller異常返回處理
1、返回錯誤頁面配置
如果此時配置有錯誤頁,那么這個時候錯誤會統一跳轉到 500 所在的路徑上進行錯誤的顯示,但是如果說現在希望能夠顯示 出錯誤更加詳細的內容呢?
所以這個時候可以單獨定義一個頁面進行錯誤的信息顯示處理,而這個頁面,可以定義在“src/main/view/templates/error.html”, 這個頁面里面要求可以輸出一些信息;
@ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) // 所有的異常都是Exception子類 public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) { // 出現異常之后會跳轉到此方法 ModelAndView mav = new ModelAndView("error"); // 設置跳轉路徑 mav.addObject("exception", e); // 將異常對象傳遞過去 mav.addObject("url", request.getRequestURL()); // 獲得請求的路徑 return mav; } }
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>
2、返回Rest錯誤信息
package cn.study.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 ; } }