Boot 4 里 server.error.include-message 为什么不生效?Whitelabel Error Page 怎么关?静态 404 页和自定义 ErrorController 各有什么限制?这篇在 Spring Boot 4.0.8 上逐项请求。
浏览器访问 /nope(Accept: text/html):404 text/html | <html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>…</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>
接口访问 /nope(Accept: application/json):404 application/json | {"timestamp":"…","status":404,"error":"Not Found","path":"/nope"}
curl 默认的 Accept(*/*)访问 /nope:404 application/json | {"timestamp":"…","status":404,"error":"Not Found","path":"/nope"}
接口访问 /boom:500 application/json | {"timestamp":"…","status":500,"error":"Internal Server Error","path":"/boom"}
接口访问 /bad:400 application/json | {"timestamp":"…","status":400,"error":"Bad Request","path":"/bad"}
接口访问 /num/abc:400 application/json | {"timestamp":"…","status":400,"error":"Bad Request","path":"/num/abc"}浏览器是 Whitelabel 页,接口是 4 字段 JSON。
server.error.include-message:标为弃用,替换为 spring.web.error.include-message
server.error.include-stacktrace:标为弃用,替换为 spring.web.error.include-stacktrace
server.error.whitelabel.enabled:标为弃用,替换为 spring.web.error.whitelabel.enabled
-- 同一节的另一组配置
接口访问 /boom,旧 key 全开:500 application/json | {"timestamp":"…","status":500,"error":"Internal Server Error","path":"/boom"}
接口访问 /bad,旧 key 全开:400 application/json | {"timestamp":"…","status":400,"error":"Bad Request","path":"/bad"}元数据标为弃用,实测不生效。
接口访问 /boom:500 application/json | {"timestamp":"…","status":500,"error":"Internal Server Error","exception":"java.lang.IllegalStateException","message":"数据库连不上了","path":"/boom"}
接口访问 /bad:400 application/json | {"timestamp":"…","status":400,"error":"Bad Request","exception":"org.springframework.web.server.ResponseStatusException","message":"id 不能为空","path":"/bad"}message 与 exception 出现在 JSON 里。
接口访问 /boom,响应体里 trace 字段:500 application/json | {"timestamp":"…","status":500,"error":"Internal Server Error","trace":"<堆栈,长度超过 1000 个字符>","path":"/boom"}include-stacktrace=always 会暴露完整堆栈。
浏览器访问 /nope:404 text/html | <html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>…</div><div>There was an unexpected error (type=Not Found, status=404).</div></body></html>
-- 同一节的另一组配置
改用 spring.web.error.whitelabel.enabled=false,浏览器访问 /nope:404 text/html | Tomcat 自带的错误页,<title>HTTP Status 404 – Not Found</title>
同上,接口访问 /nope:404 application/json | {"timestamp":"…","status":404,"error":"Not Found","path":"/nope"}新 key 才能关掉。
浏览器访问 /nope:404 text/html | <html><body>这是 static/error/404.html 里的自定义 404 页</body></html>
接口访问 /nope:404 application/json | {"timestamp":"…","status":404,"error":"Not Found","path":"/nope"}
浏览器访问 /boom(没有 5xx.html):500 text/html | <html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>…</div><div>There was an unexpected error (type=Internal Server Error, status=500).</div></body></html>按状态码匹配,只对浏览器请求生效。
浏览器访问 /nope:406 |
接口访问 /boom:500 application/json | {"状态":"500","自定义":"我自己的 /error 处理"}只产生 JSON 时,浏览器请求得到 406。