將以下代碼放置到 main方法中。然后在resources 中的static中新建404.html、405.html,這里可以自定義錯誤編碼,不局限於這兩個。
@Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return (container -> { ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"); ErrorPage error405Page = new ErrorPage(HttpStatus.METHOD_NOT_ALLOWED, "/405.html"); container.addErrorPages(error404Page, error405Page); }); }