Springboot配置404或者500等异常的状态码跳转自定义页面

有时候Springboot默认的404页面并不是能满足我们的业务需求

所以这时候需要配置一下 , 上代码


import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

/**
 * created with IntelliJ IDEA
 *
 * @author: create by limu
 * Date: 2020/7/21
 * Time:10:50
 */
@Component
public class ErrorConfig implements ErrorPageRegistrar {

    /**
     * 设置404and500页面指向
     *
     * @param registry 注册表
     */
    @Override
    public void registerErrorPages(ErrorPageRegistry registry) {
        ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/fourZeroFour");
        ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/login");
        registry.addErrorPages(error404Page, error500Page);
    }
}

fourZeroFour 就是我自定义的404页面

500的话我让他直接跳转login页面

httpStatus里面有很多状态值,大家可以点进去看一下 , 然后添加到registry里面;

重启一下服务就可以了

猜你喜欢

转载自blog.csdn.net/qq_38821574/article/details/109099681