首页 > 解决方案 > Spring Boot 2.5 中 server.error.path 的默认值是多少?

问题描述

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

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

语境:

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

标签: springspring-bootpropertiesconfigurationproperties-file

解决方案


/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

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


推荐阅读