襄阳论坛网站建设,免费百度网站建设,川畅互联咨询 网站建设,网站手机版模板1. 场景
有一项工作#xff0c;需要将数据从一个服务S中读取出来#xff08;得到的是一个JSON#xff09;#xff0c;将数据解析转换以后构造成一个数组的类型A的对象#xff0c;写入到一个服务T中。
A.class
Data
public class A
{String f0 ;String f1 ;
}在发现需要…1. 场景
有一项工作需要将数据从一个服务S中读取出来得到的是一个JSON将数据解析转换以后构造成一个数组的类型A的对象写入到一个服务T中。
A.class
Data
public class A
{String f0 ;String f1 ;
}在发现需要增加一种类型A的字类型B这个类型属性非常多将近一百在这种情形下如果直接定义出类型B来定义那么多属性还得设置将会有更大的工作量所以考虑是不是这些额外的信息可以用一个Map来存储。变成如下结构
A.class
Data
public class A
{String f0 ;String f1 ;MapString , Object otherPropMap ;
}输出的JSON
{f0: ,f1: ,op1: ,op2: ,
}2. 做法
Data
public class A
{String f0 ;String f1 ;MapString , Object otherPropMap ;JsonAnyGetterpublic MapString , String getOtherPropMap(){return otherPropMap ;}
}在jackson-annotations-2.11时JsonAnyGetter注解只能用在Method上在2.13版本中已经可以直接使用在属性上了。
3. JsonUnwrapped用法
JsonUnwrapped注解只对Bean其作用Map是不能起作用的。例如下面是可以的。
Data
public class A
{String f0 ;JsonUnwrappedB f1 ;
}// B.java
public class B
{String f3 ;String f4 ;
}那么输出结果是
{f0: ,f3: ,f4:
}