如何在 application.yaml (spring boot) 中禁用白标签错误页面 [英] How to disable whitelabel error page in application.yaml (spring boot)

查看:82
本文介绍了如何在 application.yaml (spring boot) 中禁用白标签错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个遗留的 Java 单体后端,提供 REST API(spring boot v1.5.9,gradle),我必须不时维护它.请求禁用此类错误页面:

I have a legacy Java monolith backend, serving REST APIs (spring boot v1.5.9, gradle) in hand which I have to maintain from time to time. A request came to disable such an error page:

"此应用程序没有明确的/error 映射,因此您将其视为后备.

"This application has no explicit mapping for /error, so you are seeing this as a fallback.

欧洲中部时间 2021 年 3 月 10 日星期三 13:41:12出现意外错误(类型=内部服务器错误,状态=500)."

Wed Mar 10 13:41:12 CET 2021 There was an unexpected error (type=Internal Server Error, status=500)."

我试图摆脱这条消息,但大多数建议都解释了如何在application.properties"中删除它.文件,我的项目根本没有.相反,我有 application.yaml 文件,我在其中添加了以下配置:

I tried to get rid of this message but most of the advices explain how to remove it in a "application.properties" file, which my project doesn't have at all. Instead, I have application.yaml files, where I added this configuration:

spring:
  autoconfigure:
    exclude:
      - org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration

但我仍然看到相同的错误页面.

But I still see the same error page.

其他建议告诉我在main"上做一些事情课程,不幸的是我的项目也没有.

Other advices tell me to do something on "main" class, which my project unfortunately doesn't have, either.

我也尝试实现 ErrorController 来获取我自己的错误页面,但我的控制器根本没有注册.此遗留应用程序未遵循标准项目结构建议,因此任何指向 Spring Boot 文档中示例结构的链接都无济于事.

I also tried to implement ErrorController to get my own Error page but my controller is not registered at all. Standard project structure recommendations are not followed on this legacy app, so any link to example structures on spring boot documentations will not help me.

任何建议禁用白标签错误页面或实施 ErrorController.

Any advice is appreciated to disable the whitelabel error page or implementing ErrorController.

推荐答案

如果要禁用默认的Whitelabel Error Page",可以添加:

If you want to disable default "Whitelabel Error Page", you can add:

server:
    //..optionally, some other properties
    error:
        whitelabel:
            enabled: false
    //..optionally, some other properties

在您的 application.yml 中.

但是,如果您想覆盖那个页面,而不是禁用它并显示应用服务器的默认错误页面,您有几种方法:

However, if you want to override that page, instead of disabling it and displaying application server's default error page, you have few ways:

  1. 创建您的自定义 error.html 页面,将其放在您的 templates 文件夹下(或从您的视图中提取的任何位置),它将是Spring Boot 的默认 BasicErrorController 自动选取作为通用错误页面,即 - 对于所有 HTTP 错误代码;

  1. Create your custom error.html page, place it under your templates folder (or wherever your view are being picked up from), and it will be automatically picked up by Spring Boot's default BasicErrorController as a generic error page, that is - for all HTTP error codes;

您可以更具体地为不同的 HTTP 错误代码创建不同的错误页面.
使用您要处理的 HTTP 状态代码命名您的错误文件,并将其保存在 resources\templates\error\.这样,您可能会有多个错误页面,它们将用于相应的 HTTP 错误.例如,当 HTTP 404 发生时将返回
404.html.

You can be more specific and create different error pages for different HTTP error codes.
Name your error file with the HTTP status code you want to handle and save it in resources\templates\error\. This way, you may have several error pages and they will be used for corresponding HTTP errors.
404.html, for instance, will be returned when HTTP 404 happens.

我希望这会有所帮助.

这篇关于如何在 application.yaml (spring boot) 中禁用白标签错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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