json - Json 整数被读取为 java Boolean true
问题描述
在 REST API 请求 json 正文中,我正在传递"argument1":true
它并且它可以工作。但我发现,当使用任何数字时,它都会将其转换为真。只有在显式使用时false
它才会变为假。我正在使用 Spring BootResponseEntityExceptionHandler
并@RestControllerAdvice
处理所有异常。534
转换为时如何抛出任何异常true
?
解决方案
在您的控制器中添加一个带有注释的方法@InitBinder
并提供一个自定义布尔编辑器
@InitBinder
public void initBinder(WebDataBinder webDataBinder) {
webDataBinder.registerCustomEditor(Boolean.class, new CustomBooleanEditor("true", "false", false));
}
Spring 注册一个默认的CustomBooleanEditor映射“true”、“on”、“yes”和任何非零数字作为true
(也允许空值作为)在值无效时false
抛出。IllegalArgumentException
您可以覆盖它或提供自己的实现来引发特定异常。
推荐阅读
- netbeans - 如何修复 Netbeans Glassfish 模块未部署?
- node.js - 如何使用猫鼬更新数组
- css - 纯CSS交互人体
- mysql - Generating monthly report mysql
- azure-blob-storage - 对 Azure 存储 Blob 服务的匿名下载请求不会下载上传期间配置的用户友好名称的 pdf
- r - R group by issue _ 多重组合的解决方案
- c# - WEB API Post Action Method中JSON对象到类成员的转换
- sql-server - 查找平均值,最小值,最大值,而列名存储在 sql 存储过程中的临时变量中
- android - 如何使 AndroidX 成为新 Android 项目的默认库?
- sql - 在 CTE 中实现标量函数