Spring Boot 移除错误页面 /error 节点

Spring Boot About 1,395 words

需求

移除Spring Boot默认的错误页面。

方法一:移除错误页面

配置了server.error.whitelabel.enablefalse,只是禁用了渲染Spring自带的错误页面。

并没有移除/error接口。

配置文件

server:
  error:
    whitelabel:
      enabled: false

浏览器

使用浏览器会得到500错误。

curl 或 Postman

使用curlPostman直接访问/error接口,能得到结果。

{
    "timestamp": "2023-06-27T05:52:12.409+00:00",
    "status": 999,
    "error": "None"
}

方法二:移除 /error 接口

@SpringBootApplication注解中移除ErrorMvcAutoConfiguration配置。

这样就不会注入BasicErrorController

@SpringBootApplication(exclude = {ErrorMvcAutoConfiguration.class})

返回结果

直接会等到404状态码,以及404页面html片段。

<!doctype html>
<html lang="en">

<head>
    <title>HTTP Status 404 – Not Found</title>
    <style type="text/css">
        body {
            font-family: Tahoma, Arial, sans-serif;
        }

        h1,
        h2,
        h3,
        b {
            color: white;
            background-color: #525D76;
        }

        h1 {
            font-size: 22px;
        }

        h2 {
            font-size: 16px;
        }

        h3 {
            font-size: 14px;
        }

        p {
            font-size: 12px;
        }

        a {
            color: black;
        }

        .line {
            height: 1px;
            background-color: #525D76;
            border: none;
        }
    </style>
</head>

<body>
    <h1>HTTP Status 404 – Not Found</h1>
</body>

</html>
Views: 695 · Posted: 2023-07-07

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh