404 的 Spring Boot 自定义错误页面 [英] Spring boot custom error page for 404

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

问题描述

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

i wish to have custom 404 error page .I have velocity in my classpath but I don't want to use velocity view resolver Below is my code

@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.

i am not able to redirect all the 404 to my some html in resource directory.

请帮忙

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

P.S It work if i am use velocityresolver and have error.vm in template directory.

谢谢和问候

推荐答案

您可以提供一个 EmbeddedServletContainerCustomizer 并在那里添加您的(例如静态)错误页面.参见错误处理spring boot 文档.例如:

You can provide a EmbeddedServletContainerCustomizer and add your (e.g. static) error pages there. See Error Handling in the spring boot docs. E.g:

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

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

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