SpringBoot 2.3.0.RELEASE版本后自定義404頁面,
SpringBoot 404錯誤兼容Ajax請求
ErrorController接口無getErrorPath方法
================================
©Copyright 蕃薯耀 2021-07-27
https://www.cnblogs.com/fanshuyao/
一、問題描述
在spring-boot-starter-parent 2.3.0.RELEASE版本后,org.springframework.boot.web.servlet.error.ErrorController接口已經不建議采用getErrorPath方法,在2.5.3版本后,ErrorController接口已經無getErrorPath方法(直接刪除)。
查看子類,發現有org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController這個類,默認是:
@Controller @RequestMapping("${server.error.path:${error.path:/error}}") public class BasicErrorController extends AbstractErrorController {
}
那么就可以通過server.error.path進行配置的覆蓋或者通過error.path進行配置覆蓋,如果都不配置,就默認是:/error
建議配置成:error.path=/deprecated/error,這樣自己就能定義/error的路徑
配置error.path能同時覆蓋處理頁面和Ajax(Rest)請求,配置server.error.path只能覆蓋處理頁面跳轉的請求,不能處理Ajax(Rest)請求,因為ErrorProperties默認是/error,需要通過error.path修改
二、SpringBoot 2.3.0.RELEASE版本后自定義404錯誤頁面
1、創建GlobalErrorController全局錯誤處理類
import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.lqy.common.bean.Result; /** * 全局異常處理 * 在spring-boot-starter-parent 2.3.0.RELEASE版本后,需要在application.properties增加配置:error.path=/deprecated/error,覆蓋默認的全局異常處理。 * <p>2.5.3后,ErrorController接口已經無getErrorPath方法。</p> * <p>查看子類,發現有org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController這個類,默認是:</p> * <p>@RequestMapping("${server.error.path:${error.path:/error}}")</p> * <p>那么就可以通過server.error.path進行配置的覆蓋或者通過error.path進行配置覆蓋,如果都不配置,就默認是:/error</p> * <p>建議配置成:error.path=/deprecated/error,這樣自己就能定義/error的路徑</p> * <p>配置error.path能同時覆蓋處理頁面和Ajax(Rest)請求,配置server.error.path只能覆蓋處理頁面跳轉的請求,不能處理Ajax(Rest)請求,因為ErrorProperties默認是/error,需要通過error.path修改</p> * * <p>在application.properties文件配置error.path=/deprecated/error</p> * * @see org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController * @see org.springframework.boot.web.servlet.error.ErrorController * * */ @Controller @RequestMapping public class GlobalErrorController { public final static String STATUS_CODE = "javax.servlet.error.status_code"; /** * 獲取HTTP請求狀態碼 * @param request * @return */ public HttpStatus getStatus(HttpServletRequest request) { Integer statusCode = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE); if (statusCode == null) { return HttpStatus.INTERNAL_SERVER_ERROR; } try { return HttpStatus.valueOf(statusCode); } catch (Exception ex) { return HttpStatus.INTERNAL_SERVER_ERROR; } } /** * 處理html頁面請求的錯誤 * @param request * @param response * @return * @throws IOException */ @RequestMapping(value="/error", produces = MediaType.TEXT_HTML_VALUE) public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) throws IOException { Integer statusCode = (Integer) request.getAttribute(STATUS_CODE); ModelAndView mav = new ModelAndView(); if(statusCode == 401) { mav.addObject("statusCode", 401); mav.setViewName("error/4xx"); }else if(statusCode == 403) { mav.addObject("statusCode", 403); mav.setViewName("error/4xx"); }else if(statusCode == 404) { mav.addObject("statusCode", 404); mav.setViewName("error/404"); }else { mav.addObject("statusCode", 500); mav.setViewName("error/500"); } return mav; } /** * 處理Ajax請求的錯誤 * @param request * @param response * @return * @throws IOException */ @RequestMapping(value="/error") @ResponseBody public Result error(HttpServletRequest request, HttpServletResponse response) throws IOException { Integer statusCode = (Integer) request.getAttribute(STATUS_CODE); return Result.failCode(statusCode); } }
2、覆蓋默認的錯誤請求處理
在application.properties文件配置error.path=/deprecated/error
#覆蓋默認的全局異常處理路徑,配合com.lqy.common.exception.controller.GlobalErrorController使用
error.path=/deprecated/error
三、spring-boot-starter-parent 2.3.0.RELEASE版本之前的自定義異常頁面
SpringBoot 自定義異常頁面,SpringBoot 自定義404頁面和500頁面,SpringBoot 錯誤兼容Ajax請求,見:
https://www.cnblogs.com/fanshuyao/p/14716703.html
(時間寶貴,分享不易,捐贈回饋,^_^)
================================
©Copyright 蕃薯耀 2021-07-27
https://www.cnblogs.com/fanshuyao/