【问题标题】:Spring boot: cannot return index page instead of error page not foundSpring boot:无法返回索引页面而不是找不到错误页面
【发布时间】:2017-03-30 16:01:18
【问题描述】:

我正在使用 spring boot 后端和 angular2 前端构建 web 应用程序,如果访问了非 root url,spring boot 返回 whitelabel 页面(因为 servlet 容器不知道角度路由),所以我正在尝试替换带有 index.html 页面的 whitelabel 页面。

在阅读了一些关于此的文章后,我尝试了这个,但没有工作

@Component
public class CustomizationBean
 implements EmbeddedServletContainerCustomizer {

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

index.html 放在 src/main/resources/static 下

我错过了什么吗?

【问题讨论】:

    标签: angular spring-boot


    【解决方案1】:

    上面的代码可以很好地显示index.html页面(而不是白标错误),但是您需要确保您的CustomizationBean在启动@ComponentScan(basePackages = { "com.yourproject"})期间可用于扫描Spring容器

    据我所知,在 spring boot 中,扫描是自动的,我有我的 带有@Component 的CustomizationBean 注解。

    Spring boot,默认情况下仅扫描 myproject 包下的类(ApplicationLauncher main 类所在的位置),如本文档 here 中所述。其余包需要在@ComponentScan中指定

    【讨论】:

    • 我用的是spring boot,不处理这个吗?
    • 其实我在添加错误页面的那一行上放了一个断点,该行正在被执行
    • 如果你的Component存在于主launcher类包中,会小心,否则需要添加@ComponentScan
    • 据我所知,在 Spring Boot 中,扫描是自动的,我的 CustomizationBean 注释带有 @Component,这还不够吗,反正我把类移到了主启动器类的包中和一样的
    猜你喜欢
    • 2018-07-27
    • 2020-11-13
    • 2018-05-07
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    相关资源
    最近更新 更多