鍍金池/ 問答/Java  HTML/ Spring boot 自定義404錯(cuò)誤頁面 @ControllerAdvice

Spring boot 自定義404錯(cuò)誤頁面 @ControllerAdvice方式

其他錯(cuò)誤都會(huì)進(jìn)入到這里,唯有404錯(cuò)誤不進(jìn)來?網(wǎng)上文檔文章都說這樣玩的,真奇怪。

404頁面一直報(bào)這個(gè)錯(cuò)誤,不進(jìn)入我的自定義設(shè)置
**Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Jun 03 11:50:14 CST 2018
There was an unexpected error (type=Not Found, status=404).
No message available**

@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    public static final String DEFAULT_ERROR_VIEW = "error/400";

    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) {
        logger.info("系統(tǒng)異常", e);
        ModelAndView m = new ModelAndView();
        m.addObject("exception", e);
        m.addObject("url", request.getRequestURL());
        m.setViewName(DEFAULT_ERROR_VIEW);
        return m;
    }
}    

如果配置了,可以捕獲404

出現(xiàn)錯(cuò)誤時(shí), 直接拋出異常

spring.mvc.throw-exception-if-no-handler-found=true

不要為我們工程中的資源文件建立映射

spring.resources.add-mappings=false

但是css js什么的都無法訪問了。

回答
編輯回答
檸檬藍(lán)
  public static final String DEFAULT_ERROR_VIEW = "error/400";

檢查一下路徑和頁面的位置

2018年7月14日 15:30