Spring Boot-具有独立tomcat的自定义404页面 [英] Spring Boot - custom 404 page with standalone tomcat

查看:999
本文介绍了Spring Boot-具有独立tomcat的自定义404页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个独立的tomcat实例中运行一个Spring Boot应用程序,并且我试图覆盖错误页面.据我了解,Spring提供了一个过滤器ErrorPageFilter ,该功能使我可以正常设置错误页面,以供Springs EmbeddedServletContainerCustomizer正确处理此情况.

I am running a Spring boot application inside a standalone tomcat instance, and I am trying to override the error pages. From my understanding, Spring provides a filter ErrorPageFilter that allows me to just setup error pages as normal for Springs EmbeddedServletContainerCustomizer to handle this case exactly.

所以我在一个类中有标准的自动配置/servlet初始化程序:

So I have my standard auto configuration/servlet initializer in one class:

@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude = [ GroovyTemplateAutoConfiguration, SecurityAutoConfiguration, ErrorMvcAutoConfiguration, JmxAutoConfiguration ] )
class Application extends SpringBootServletInitializer {

    @Override protected SpringApplicationBuilder configure( SpringApplicationBuilder application ) {
        application.sources( Application )
    }

(我在自动配置和servlet初始化中使用了相同的类,这就是为什么我只是在configure方法中传递了Application类的原因)

(I am using the same class for autoconfiguration and servlet init, which is why i just pass my Application class in the configure method)

查看

Looking at the source code for SpringBootServletInitializer it looks like the ErrorPageFilter class is being added by just extending that class here. I have turned off the ErrorMvcAutoConfiguration - but again, looking at that source code it looks like that is just setting default error pages and not actually setting anything up with the ErrorPageFilter.

然后我有了我的错误配置文件:

I then have my error config file:

@Configuration
class ErrorConfiguration implements EmbeddedServletContainerCustomizer {

    @Override public void customize( ConfigurableEmbeddedServletContainer container ) {
        container.addErrorPages(new ErrorPage( HttpStatus.NOT_FOUND, "/errors/404" ))
    }

但是,如果我只是访问无效的URL,而我DispatcherServlet找不到匹配项,那么我只会得到tomcats/404.html-不是我的视图链接到"/errors/404"(我已将此路径映射到到百里香叶视图模板,效果很好-如果我导航到/errors/404,它将显示确定)

However, if I just visit an invalid URL, and I DispatcherServlet can't find a match then I just get tomcats /404.html - not my view linked to "/errors/404" (I have this path mapped to a thymeleaf view template, that works fine - if I navigate to /errors/404 it displays ok)

有什么想法为什么我的自定义错误页面不起作用?跟踪日志,我得到一行关于正在配置ErrorPageFilter并在应用程序启动时可以正常安装的信息,但是没有提及当请求进入时过滤器会做任何事情.

Any ideas why my custom error page is not working? tracing the logs, I get a line about the ErrorPageFilter being configured and setup ok on application startup, but then no mentions of the filter doing anything when a request comes in.

推荐答案

对于较早版本的Spring Boot(0.5.x),可以使用以下代码

You can use following code for older versions of 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"));
   }
}

较新的春季启动版本(1.X.RELEASE)围绕ServerProperty进行了一些重构.见下文

Newer spring boot versions (1.X.RELEASE) has some refactoring around ServerProperties. See below,

  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"));
  }

}

然后定义一个用于注入ServerProperies的bean.

Then define a bean to inject ServerProperies.

@Bean
public ServerProperties getServerProperties() {
    return new ServerCustomization();
}

git

非常重要:如果使用maven进行构建,则必须将所有资源文件存储在src/main/resources文件夹下.否则,maven不会将这些文件添加到最终的jar工件中.

Very Important: If you are using maven to build, You must store all the resource files under src/main/resources folder. Otherwise maven will not add those files to final jar artifact.

这篇关于Spring Boot-具有独立tomcat的自定义404页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆