【问题标题】:spring boot custom error pages do not render when deployed on serverspring boot 自定义错误页面在服务器上部署时不呈现
【发布时间】:2019-08-17 14:34:29
【问题描述】:

根据文档,我为 Spring Boot 应用程序 404.html 编写了一个自定义错误页面,并将此文件放在 src/main/resources/public/error/404.html 中。我的 application.properties 文件也包含“server.error.whitelabel.enabled=false”。现在在 localhost 上,这个 404.html 被渲染,但是当部署在服务器上时,这个页面不会渲染,而是显示 tomcat HTTP 404 错误页面。谁能建议我如何解决这个问题。

PS : 文件 404.html, 404.css 在服务器上部署的战争中被捆绑在 WEB-INF 文件夹中。

【问题讨论】:

    标签: spring-boot tomcat custom-error-pages


    【解决方案1】:

    我之前也有类似的问题,我只是在 application.properties 文件中添加了这些配置

    # Spring
    spring.mvc.throw-exception-if-no-handler-found=true
    spring.resources.add-mappings=false
    

    注意:使用此配置。在部署在服务器上的应用程序上。这将使您的应用程序在本地(嵌入式tomcat)上使用spring boot时忽略资源

    【讨论】:

    • 好的,谢谢!您是否添加了任何控制器来处理此异常或仅在 src/main/resources/public/error/404.html 中添加了自定义错误 html 文件?
    • 我有一个类有@controlleradvice 来处理全局异常。 baeldung.com/exception-handling-for-rest-with-spring
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 2020-06-29
    • 1970-01-01
    • 2019-09-27
    • 2018-06-26
    相关资源
    最近更新 更多