Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 404的Spring引导自定义错误页_Spring Boot - Fatal编程技术网

Spring boot 404的Spring引导自定义错误页

Spring boot 404的Spring引导自定义错误页,spring-boot,Spring Boot,我希望有自定义404错误页面。我的类路径中有velocity,但我不想使用velocity视图解析器 下面是我的代码 @EnableAutoConfiguration(exclude={VelocityAutoConfiguration.class}) @ComponentScan public class Application { Properties props = new Properties(); public static void main(String[] args) {

我希望有自定义404错误页面。我的类路径中有velocity,但我不想使用velocity视图解析器 下面是我的代码

@EnableAutoConfiguration(exclude={VelocityAutoConfiguration.class})
@ComponentScan
public class Application {
Properties props = new Properties();

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}
}
我无法将所有404重定向到资源目录中的某个html

请协助

如果我使用velocityresolver并且在模板目录中有error.vm,它就可以工作


谢谢

您可以提供一个
EmbeddedServletContainerCustomizer
并在那里添加您的(例如静态)错误页面。请参阅spring引导文档中的。例如:

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));
            }
        };
}

从弹簧靴开始
1.4.x

如果要显示给定状态代码的自定义HTML错误页,可以将文件添加到
/error
文件夹中。错误页面可以是静态HTML(即添加到任何静态资源文件夹下),也可以使用模板构建。文件名应为准确的状态代码(例如
404
)或序列掩码(例如
5xx

例如,要将
404
映射到静态HTML文件,您的文件夹结构如下所示:

src/
 +- main/
     +- java/
     |   + <source code>
     +- resources/
         +- public/
             +- error/
             |   +- 404.html
             +- <other public assets>
src/
 +- main/
     +- java/
     |   + <source code>
     +- resources/
         +- template/
             +- error/
             |   +- 5xx.ftl
             +- <other templates>

查看以获得更详细的讨论。

谢谢,它起了作用,尽管我尝试了同样的方法,但以前没有起作用(花了几个小时把头撞在墙上)。我想你需要一个静态文件夹才能有htmls,我的404.html在资源文件夹中。是的,静态文件必须在静态文件夹中。但是,您可以在其他位置配置静态文件,也可以在此处使用视图(而不是静态文件)。@cfrick,您如何使用视图而不是静态文件?
EmbeddedServletContainerCustomizer
在其他库中可用吗?等等,春季MVC?