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/Shiro ---登陆成功后返回登陆前界面<页面重定向>
Spring security ---登陆成功后返回登陆前界面<页面重定向> 问题:在登陆/退出成功后,我们往往通过http.formLogin().successForwardUrl() ...
- 鸿蒙os执行效率最高的是多少级,华为鸿蒙OS升级半个多月后,好评度虽高,但“差评”也很扎心!...
原标题:华为鸿蒙OS升级半个多月后,好评度虽高,但"差评"也很扎心! 中美科技竞争的不断升温,鸿蒙操作系统,成了华为面临"十面封锁"中的一道亮光,不仅能让华为在 ...
- mybatis动态查询(分页排序搜索)+分解关联查询+Logback 日志配置(打印sql到控制台)+mybatis新增记录后返回自增的id。批量=11/2~11/20
一.mybatis动态查询(分页排序搜索) mybatis框架分页实现,有几种方式,最简单的就是利用原生的sql关键字limit来实现,还有一种就是利用interceptor来拼接sql,实现和lim ...
- SpringMVC拦截器HandlerInterceptor拦截后返回数据或视图View
SpringBoot版本:2.1.6.RELEASE SpringMVC版本:5.1.8.RELEASE SpringMVC拦截器 比如说在SpringMVC Web环境下,需要实现一个权限拦截的功能 ...
- 计算机硬件升级的说法有哪些,客户让我给他的电脑升级,看到电脑配置后:看来升级又凉凉了!...
原标题:客户让我给他的电脑升级,看到电脑配置后:看来升级又凉凉了! 电脑升级指的是通过增加或者替换硬件的方式提高电脑的性能,电脑升级常见的就是升级硬盘与内存条,因为这两个硬件适用于所有的电脑,但是电脑 ...
- group by 分组后 返回的是一个同属性的集合
group by 分组后 返回的是一个同属性的集合 我们可以遍历该集合 转载于:https://www.cnblogs.com/classmethond/p/10164326.html
- ACMNO.14一球从M米高度自由下落,每次落地后返回原高度的一半,再落下。 它在第N次落地时反弹多高?共经过多少米? 保留两位小数 输入 M N 输出 它在第N次落地时反弹多高?共经过多少米
题目描述 一球从M米高度自由下落,每次落地后返回原高度的一半,再落下.它在第N次落地时反弹多高?共经过多少米? 保留两位小数 输入 M N 输出 它在第N次落地时反弹多高?共经过多少米? 保留两位小数 ...
- android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序
android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序 在应用里使用了后台服务,并且在通知栏推送了消息,希望点击这个消息回到activity, ...
- 网格搜索后返回的就是最佳的模型
网格搜索后返回的就是最佳的模型 https://www.cnblogs.com/tszr/p/10802661.html clf=RandomizedSearchCV(LogisticRegress ...
最新文章
- 【调试工具】tcpdump
- 网络工程原理与实践教程实验安排
- ElasticSearch入门系列(三)文档,索引,搜索和聚合
- jzoj3919-志愿者【换根法,线段树,树形dp】
- fgets阻塞 stdin 退出_来自stdin问题的fgets[c]
- Xception,Inception-ResNet,SENet(Squeeze-and-Excitation)
- 三次多项式曲线php,多项式计算的效率测试,多项式计算效率_PHP教程
- 浅谈 Mybatis中的 ${ } 和 #{ }的区别
- pandas 选取第一行_用pandas中的DataFrame时选取行或列的方法
- Json 语法 格式
- Redis报错#MISCONF Redis is configured to save RDB snapshots, but is currently not
- layoutSubview 总结
- 回文字符串,回文链表
- font awesome图标大小调整
- 用PS制作透明背景的电子签名
- sub1G-CC1310的应用开发之入门
- Oracle aes128和aes256加解密
- MacOS 安装 MySQL
- 华为EMUI屏蔽更新
- 2021年全球超导体收入大约735.7百万美元,预计2028年达到823.7百万美元