Springboot configures abnormal status codes such as 404 or 500 to jump to a custom page

Sometimes the default 404 page of Springboot is not able to meet our business needs

So you need to configure it at this time, and upload the code


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 is my custom 404 page

If 500, I will let him jump directly to the login page

There are many status values ​​in httpStatus, you can click in to take a look, and then add to the registry;

Just restart the service

 

 

Guess you like

Origin blog.csdn.net/qq_38821574/article/details/109099681