美文网首页
SpringBoot 错误页配置

SpringBoot 错误页配置

作者: 编程喵喵 | 来源:发表于2019-08-18 22:15 被阅读0次

    一、spring1.x中处理方式

    @Bean
        public EmbeddedServletContainerCustomizer containerCustomizer() {
            return new EmbeddedServletContainerCustomizer() {
                @Override
                public void customize(ConfigurableEmbeddedServletContainer container) {
                    ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401");
                    ErrorPage error405Page = new ErrorPage(HttpStatus.METHOD_NOT_ALLOWED, "/405");
                    ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404");
                    ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500");
     
                    container.addErrorPages(error401Page,error405Page, error404Page, error500Page);
                }
            };
        }
    

    二、spring2.x中处理方式

    @Component
    public class ErrorConfig implements ErrorPageRegistrar {
     
        @Override
        public void registerErrorPages(ErrorPageRegistry registry) {
            ErrorPage error400Page = new ErrorPage(HttpStatus.BAD_REQUEST, "/error400Page");
            ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/error401Page");
            ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/error404Page");
            ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error500Page");
    
            registry.addErrorPages(error400Page,error401Page,error404Page,error500Page);
        }
    }
    

    相关文章

      网友评论

          本文标题:SpringBoot 错误页配置

          本文链接:https://www.haomeiwen.com/subject/kpidsctx.html