Springboot 全域性錯誤頁面
方法一 :框架預設的錯誤頁面處理
只需要在resource下面建立public error資料夾即可,無需任何其他操作
方法二:
定義錯誤頁面的配置類
@Configuration
public class ErrprPageConfig {
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public
ConfigurableEmbeddedServletContainer container) {
ErrorPage errorPage400 = new ErrorPage(HttpStatus.BAD_REQUEST,//設定錯誤類
"/error/400");//get 請求的路徑
ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND,
"/error/404"
ErrorPage errorPage500 = new ErrorPage(
HttpStatus.INTERNAL_SERVER_ERROR, "/error/500");
container.addErrorPages(errorPage400, errorPage404,
errorPage500);
}
};
}
}
定義頁面跳轉的controller
@RequestMapping
public String error(@PathVariable int code) {
String pager = "error/"+code;
return pager;
}
定義錯誤頁面位置