开始定义配置类
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
/**
*定义配置类
*/
@Configuration
public class ErrorConfigurar implements ErrorPageRegistrar {
@Override
public void registerErrorPages(ErrorPageRegistry registry) {
ErrorPage[] errorPages = new ErrorPage[2];
errorPages[0] = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
errorPages[1] = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
registry.addErrorPages(errorPages);
}
}
1、404.html一定要建在template/static下面,否则无效。
2、new ErrorPage可以重定向具体的html也可以重定向请求地址,如/404,那就需要添加/404的Controller
讲解:
其中配置类实现了ErrorPageRegistrar接口
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.boot.web.server;
@FunctionalInterface
public interface ErrorPageRegistrar {
void registerErrorPages(ErrorPageRegistry registry);
}
我们的请求找不到时,会跳到错误页面,配置类的可以截取404页面从而进行友好的跳转自己的404页面
如果发现博文中有错误,请各位大佬指点指点,谢谢观看
博客介绍了在Spring Boot中,当请求找不到时会跳到错误页面。可通过定义配置类截取404页面,实现友好地跳转到自定义的404页面。
1万+

被折叠的 条评论
为什么被折叠?



