spring boot配置404 和 500错误页面跳转

我是用的spring boot版本是v2.1.4.RELEASE

添加以下代码即可对404和500进行重定向:

package com.handler;

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;
/**
 * @author lin.hongwen
 * @date 2021-03-29
 * @Description
 */
@Configuration
public class ErrorConfigurar implements ErrorPageRegistrar {

    @Override
    public void registerErrorPages(ErrorPageRegistry registry) {
        ErrorPage[] errorPages = new ErrorPage[2];
        errorPages[0] = new ErrorPage(HttpStatus.NOT_FOUND, "/manage/index.html");
        errorPages[1] = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500.html");
        registry.addErrorPages(errorPages);
    }
}

 

posted @ 2021-03-29 10:20  林被熊烟岛  阅读(571)  评论(0编辑  收藏  举报