EmbeddedServletContainerCustomizer(嵌入式servlet容器定制)这个在spring boot2.X的版本中就不再提供支持了貌似2.0版本还能用 ,用来提供对异常的处理,配置修改servlet。在支持EmbeddedServletContainerCustomizer的springboot版本中我们可以类似这样来配置异常处理和跳转
package com.dabai.springtest.error; import org.springframework.boot.web.server.ErrorPage; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpStatus; @Configuration public class ErrorPageConfig { @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); } }; } }
在不支持的情况下需要这样
package com.dabai.springtest.error; 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; @Component public class ErrorPageConfig implements ErrorPageRegistrar { @Override public void registerErrorPages(ErrorPageRegistry registry) { ErrorPage error400Page=new ErrorPage(HttpStatus.BAD_REQUEST,"/error400" ); ErrorPage error401Page=new ErrorPage(HttpStatus.UNAUTHORIZED,"/error401"); ErrorPage error500Page=new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,"/error500"); registry.addErrorPages(error400Page,error401Page,error500Page); } }
其中
ErrorPageRegistrar接口:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.springframework.boot.web.server; @FunctionalInterface public interface ErrorPageRegistrar { void registerErrorPages(ErrorPageRegistry registry); }
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.springframework.boot.web.server; @FunctionalInterface public interface ErrorPageRegistry { void addErrorPages(ErrorPage... errorPages); }
如果是在不支持EmbeddedServletContainerCustomizer的SpringBoot版本中希望制定容器的一些特性可以使用接口:WebServerFactoryCustomizer
package com.dabai.bishe.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.context.annotation.Configuration; import java.io.File; //@Configuration public class WebConfig { private static final Logger logger = LoggerFactory.getLogger(WebConfig.class); @Configuration public static class FrontEnd implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { @Value("${web.front.baseDir}") private File baseDir; @Override public void customize(ConfigurableServletWebServerFactory factory) { if (!baseDir.exists()) { if (!baseDir.mkdir()) { logger.info("create web.front base path:" + baseDir + " failed!already exists!"); } else { logger.info("create web.front base path:" + baseDir + " success!"); } } factory.setDocumentRoot(baseDir); } } }