当前位置:网站首页>If the value of the enum map does not exist, deserialization is not performed
If the value of the enum map does not exist, deserialization is not performed
2022-07-31 11:07:00 【Bo·Dream】
文章目录
1. 前言
- Enumerations are often used in development,when using it,It may happen that the passed value is not among the values defined by the enumeration,则报错如下(无法转换):
- The reason for this isThe corresponding value does not exist in the enumeration,Therefore, normal object conversion assignment cannot be performed,There are generally two places where the scene often occurs:
一、controllerWhen the layer interface is called externally;
二、mybatisWhen the query result is deserialized. - I had an idea one day,Why when deserializing,No value is passed in and not when enumerating the mapped values,不报错,自动赋值为null呢?
2. 先说结论
在controllerWhen the layer interface receives parameters,是使用objectMapper进行反序列化的,And there is a configuration inside:
READ_UNKNOWN_ENUM_VALUES_AS_NULL
,意思就是:Get an unrecognized enumeration value and assign it tonull,不报错;
在springboot中可在yml配置文件中写:spring: jackson: deserialization: READ_UNKNOWN_ENUM_VALUES_AS_NULL: true
在mybatisWhen the query results are reversed,可以使用mybatis的类型处理器TypeHandler,to judge when deserializing.
在配置文件中指定The default enumeration type handler:mybatis: configuration: default-enum-type-handler: com.example.csdn.enum_test.MyEnumTypeHandler
// 自定义枚举处理器,继承mybatis自带的EnumTypeHandler,Simplify the code to be written for optimization // @MappedTypes is meant to indicate thatTypeHandlerThe implementation class can handleJava 类型的集合 // @MappedTypes({Enum.class})的意思是:Only the enumeration type will go to the following processor @MappedTypes({ Enum.class}) public class MyEnumTypeHandler<E extends Enum<E>> extends EnumTypeHandler<E> { // corresponding to the enumeration typeclass对象 private final Class<E> type; public MyEnumTypeHandler(Class<E> type) { super(type); this.type = type; } @Override public E getNullableResult(ResultSet rs, String columnName) throws SQLException { String s = rs.getString(columnName); // Get all the enumeration values final Field[] declaredFields = type.getFields(); // If the value is taken from the library,One of the enumeration values exists // 则 directly to enumeration,否则,directly responsible fornull return Arrays.stream(declaredFields).anyMatch(field -> field.getName().equals(s)) ? Enum.valueOf(type, s) : null; } }
解释一下: Those who really have this need can consider using the above method,This operation is not recommended in other cases,After all, if the other party passed on something inexplicable,You also don't know what illegal content the other party is passing,直接转null了,而且在controller层dtoAn enumeration is defined in the object,Also a check,directly against the rules,error earlier.
3. 代码验证
1. controllerThe layer interface encountered an enumeration map value that does not exist
在配置文件中配置:
spring: jackson: deserialization: READ_UNKNOWN_ENUM_VALUES_AS_NULL: true
代码:
@Data public class testDto { private String name; private test_enum pwd; } public enum test_enum { A, B, C, } @RestController public class EnumController { @PostMapping("/enum/test") public void test2(@RequestBody testDto testDto){ System.out.println(testDto); } }
2. mybatisThe enum map value in does not exist
在Without a custom type handler下:
In the case of adding a custom processor:
配置文件中配置:mybatis: configuration: default-enum-type-handler: com.example.csdn.enum_test.MyEnumTypeHandler
@MappedTypes({ Enum.class}) public class MyEnumTypeHandler<E extends Enum<E>> extends EnumTypeHandler<E> { // corresponding to the enumeration typeclass对象 private final Class<E> type; public MyEnumTypeHandler(Class<E> type) { super(type); this.type = type; } @Override public E getNullableResult(ResultSet rs, String columnName) throws SQLException { String s = rs.getString(columnName); // Get all the enumeration values final Field[] declaredFields = type.getFields(); // If the value is taken from the library,One of the enumeration values exists // 则 directly to enumeration,否则,directly responsible fornull return Arrays.stream(declaredFields).anyMatch(field -> field.getName().equals(s)) ? Enum.valueOf(type, s) : null; } }
边栏推荐
- 数据持久化技术——MP
- 强大的SQL计算利器-SPL
- "JUC Concurrent Programming - Advanced" 06 - Immutability of Shared Models (Design of Immutable Classes | Use of Immutable Classes | Flyweight Pattern)
- SQL学习笔记——REGEXP运算符
- 使用内存映射加快PyTorch数据集的读取
- Three ways of single sign-on
- Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
- 《JUC并发编程 - 高级篇》06 - 共享模型之不可变(不可变类的设计 | 不可变类的使用 | 享元模式)
- SQL如何从字符串截取指定字符(LEFT、MID、RIGHT三大函数)
- Many mock tools, this time I chose the right one
猜你喜欢
随机推荐
MySQL row-level locks (row locks, adjacent key locks, gap locks)
众多mock工具,这一次我选对了
5 个开源的 Rust Web 开发框架,你选择哪个?
deeplab实现自己遥感地质分割数据集
2022/7/30
sql力扣刷题八
矩形脉冲波形的占空比及脉冲和瞬态特征的测量
mysql 索引使用与优化
Sql optimization summary!detailed!(Required for the latest interview in 2021)
PyQt5快速开发与实战 9.5 PyQtGraph在PyQt中的应用 && 9.6 Plotly在PyQt中的应用
实现弹框组件
蓝牙协议栈开发板 STM32F1 跑蓝牙协议栈 –传统蓝牙搜索演示以及实现原理[通俗易懂]
文件包含漏洞
分布式事务Seata详细使用教程
医院管理系统数据库,课程设计,SQLserver,纯代码设计
瑞吉外卖项目:新增菜品与菜品分页查询
双链表的插入和删除
一、excel转pdf格式jacob.jar
Creation of doubly linked list
The principle of v-model