sb2.0统一错误页面

背景

调试mybatis-plus分页时,500错误,直接显示了原有的报错页。

实操步骤

  1. 重写错误跳转

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    @Configuration
    public class ErrorConfigurar {
    /**
    * SpringBoot2.0以上版本WebServerFactoryCustomizer代替之前版本的EmbeddedWebServerFactoryCustomizerAutoConfiguration
    *
    * @return
    */

    //@Bean必须加上
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
    //第二种写法:java8 lambda写法
    return (factory -> {
    ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND, "/error.do");
    ErrorPage errorPage500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error.do");
    factory.addErrorPages(errorPage404, errorPage500);
    });
    }
    }
  2. 自定义错误跳转(普通controller,用于给第一步跳转)

    https://github.com/moon-zhou/SpringBoot_Backend_Modules/blob/master/backend-base-web/src/main/java/org/moonzhou/backend/base/web/controller/MyErrorController.java

  3. 编写第二步里的相关页面

    https://github.com/moon-zhou/SpringBoot_Backend_Modules/tree/master/backend-base-war/src/main/webapp/templates/error