且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Jackson映射器如何知道每个Json对象中的哪个字段要分配给类对象?

更新时间:2023-01-16 12:21:41

没有注释:



没有任何注释,它执行所谓的 POJO 映射,它只使用反射,并使用一些有关如何将json中的键映射到实例成员名称的规则。 * 注意:它适用于私有成员以及 public 包受保护以及

Without Annotations:

Without any annotations, it does what is called POJO mapping, it just uses reflection on the instance members and uses some rules about how to map the keys in the json to the names of the instance members. *note: it works on private members as well as public or package protected as well

如果它与实例成员的名称不匹配,则它开始尝试匹配 getXXX setXXX 方法,如果它不匹配则放弃。

If it doesn't match the names of the instance members, then it starts trying to match the getXXX and setXXX methods, if it doesn't match anything then it gives up.

它使用注释提供的元数据进行映射和转换。

It uses the metadata supplied by the annotations to do the mapping and conversions.

当你有源添加它时,明确地使用注释总是更好,然后对于什么被映射到什么没有猜测。

It is always better to explicitly use the annotations when you have the source to add them to, then there is no guess work on what gets mapped to what.

记住显式总是优于隐式!

映射注释

我现在正在为我的所有新项目创建JSON模式定义,以根据模式规则引擎记录什么是有效的JSON。这是记录数据结构和消除解析错误的好方法。

I am creating JSON Schema definitions for all my new projects now to document what is and isn't valid JSON according to the schema rules engine. It is a great way to document your data structures and eliminate parsing errors.