您可以将以下代码用于旧版本的Spring Boot(0.5.x)
public class ServerCustomization extends ServerProperties { @Override public void customize(ConfigurableEmbeddedServletContainerFactory factory) { super.customize(factory); factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/yourpath/error-not-found.jsp")); factory.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/yourpath/error-internal.jsp")); factory.addErrorPages(new ErrorPage("/yourpath/error-other.jsp")); }}较新的spring启动版本(1.X.RELEASE)围绕ServerProperty进行了一些重构。见下文,
public class ServerCustomization extends ServerProperties { @Override public void customize(ConfigurableEmbeddedServletContainer container) { super.customize(container); container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/jsp/404.jsp")); container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/jsp/500.jsp")); container.addErrorPages(new ErrorPage("/jsp/error.jsp")); }}然后定义一个bean注入ServerProperies。
@Beanpublic ServerProperties getServerProperties() { return new ServerCustomization();}样本项目发布在git中
非常重要:如果使用maven进行构建,则必须将所有资源文件存储在src / main /
resources文件夹下。否则,maven不会将这些文件添加到最终的jar工件中。



