【问题标题】:What is the default value of server.error.path in Spring Boot 2.5?Spring Boot 2.5 中 server.error.path 的默认值是多少?
【发布时间】:2021-08-30 01:53:17
【问题描述】:

在 Spring Boot 中,application.properties 文件中 server.error.path 属性的默认值是多少?

如果我希望它是“/error”,我是否需要定义它,或者它是自动的“/error”,并且只有当我希望它与它不同时才必须定义它?

上下文:

不推荐使用 ErrorController 的 getErrorPath(),如果我的值为“/error”,我认为不需要在 application.properties 中专门设置 server.error.path。

【问题讨论】:

    标签: spring spring-boot properties configuration properties-file


    【解决方案1】:

    /error 是默认设置,您不需要在配置中做任何额外的事情。 Spring Boot 通常会选择合理的默认值。如果您需要自定义错误页面的显示,请将文件 error.html 添加到您的 src/main/resources/templates 文件夹中。我在我的项目中加入了 Thymeleaf,这是一个例子:

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
      <meta charset="UTF-8">
      <title>Error</title>
      <link href="../static/css/bootstrap.css" rel="stylesheet" th:href="@{/css/bootstrap.css}"/>
    </head>
    <body>
    <div class="container">
      <h3>Error Occurred - Details Below</h3>
      <table class="table">
        <tr>
          <td>Date</td>
          <td th:text="${timestamp}"/>
        </tr>
        <tr>
          <td>Path</td>
          <td th:text="${path}"/>
        </tr>
        <tr>
          <td>Error</td>
          <td th:text="${error}"/>
        </tr>
        <tr>
          <td>Status</td>
          <td th:text="${status}"/>
        </tr>
        <tr>
          <td>Message</td>
          <td th:text="${message}"/>
        </tr>
        <tr>
          <td>Exception</td>
          <td th:text="${exception}"/>
        </tr>
        <tr>
          <td>Trace</td>
          <td>
            <pre th:text="${trace}"/>
          </td>
        </tr>
      </table>
    </div>
    </body>
    </html>
    

    您还可以调整 Spring Boot 的其他错误设置。以下是一些(YML 示例):

    server:
      error:
       include-exception: true
       include-stacktrace: always
       include-message: always
    

    我将此设置仅用于开发(开发配置文件)。我通常不会为生产(产品配置文件)显示异常或堆栈跟踪。

    【讨论】:

      猜你喜欢
      • 2022-08-03
      • 1970-01-01
      • 2014-10-20
      • 2018-07-12
      • 2015-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      相关资源
      最近更新 更多