當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot升级到2.3.x后返回message为空
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot升级到2.3.x后返回message为空
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、背景
SpringBoot項目版本升級:2.1.6.RELEASE -> 2.3.9.RELEASE
版本升級后,原有一些自定義異常的的錯誤message無法返回了。
// 預期結果 {"timestamp": "2021-03-02T06:36:09.458+00:00","status": 500,"error": "Internal Server Error","message": "用戶名已存在","path": "/user" }// 實際結果,message為空 {"timestamp": "2021-03-02T06:36:09.458+00:00","status": 500,"error": "Internal Server Error","message": "","path": "user" }二、解決
在配置文件中添加如下:
server:error:include-message: ALWAYS三、擴展
- 3.1 什么時候引入的這個配置?
在翻閱了SpringBoot的官方文檔后,發現在2.3.0.RELEASE中引入了這個配置。
2.3.0.RELEASE版本的Release Note是這樣寫的:
其主要原因是防止泄露敏感信息到客戶端。并且默認情況下就是不返回message字段的。
- 3.2 源碼分析
在org.springframework.boot.autoconfigure.web.ServerProperties的error屬性配置如下:
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) public class ServerProperties {// ...@NestedConfigurationPropertyprivate final ErrorProperties error = new ErrorProperties();// ... }ErrorProperties如下:在下面的類中可以看到定義了多個屬性,用于確定某些屬性是否返回到client。
package org.springframework.boot.autoconfigure.web;/*** Configuration properties for web error handling.*/ public class ErrorProperties {/*** Path of the error controller.*/@Value("${error.path:/error}")private String path = "/error";/*** Include the "exception" attribute.*/// 要點1:是否包含exception屬性private boolean includeException;/*** When to include the "trace" attribute.*/// 要點2:是否包含trace屬性private IncludeStacktrace includeStacktrace = IncludeStacktrace.NEVER;/*** When to include "message" attribute.*/// 要點3:是否包含message屬性private IncludeAttribute includeMessage = IncludeAttribute.NEVER;/*** When to include "errors" attribute.*/// 要點4:是否包含errors屬性private IncludeAttribute includeBindingErrors = IncludeAttribute.NEVER;private final Whitelabel whitelabel = new Whitelabel();// ... }搜索源碼, 可以看到在org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController中會用到它:
@Controller @RequestMapping("${server.error.path:${error.path:/error}}") public class BasicErrorController extends AbstractErrorController {private final ErrorProperties errorProperties;// ...@RequestMappingpublic ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {HttpStatus status = getStatus(request);if (status == HttpStatus.NO_CONTENT) {return new ResponseEntity<>(status);}// 要點1:getErrorAttributes方法就是要返回錯誤屬性信息Map<String, Object> body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));return new ResponseEntity<>(body, status);}// 要點2:父類中的getErrorAttributes方法protected Map<String, Object> getErrorAttributes(HttpServletRequest request, ErrorAttributeOptions options) {WebRequest webRequest = new ServletWebRequest(request);return this.errorAttributes.getErrorAttributes(webRequest, options);}// 要點3:errorAttributes的默認實現類(DefaultErrorAttributes)中的getErrorAttributes方法@Overridepublic Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {// 要點4:這里的處理邏輯是有這個error配置,就返回這個屬性。如果沒有配置include這個屬性,就移除這個屬性或者置空Map<String, Object> errorAttributes = getErrorAttributes(webRequest, options.isIncluded(Include.STACK_TRACE));if (Boolean.TRUE.equals(this.includeException)) {options = options.including(Include.EXCEPTION);}if (!options.isIncluded(Include.EXCEPTION)) {errorAttributes.remove("exception");}if (!options.isIncluded(Include.STACK_TRACE)) {errorAttributes.remove("trace");}// 要點5:默認情況下是NEVER配置,所以返回的是空串""if (!options.isIncluded(Include.MESSAGE) && errorAttributes.get("message") != null) {errorAttributes.put("message", "");}if (!options.isIncluded(Include.BINDING_ERRORS)) {errorAttributes.remove("errors");}return errorAttributes;}// ... }文章轉自
總結
以上是生活随笔為你收集整理的SpringBoot升级到2.3.x后返回message为空的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring security认证的底层
- 下一篇: 【干货】打造优秀B端产品需求分析流程要点