objectMaaper 反序列化json字段多于或少于实体处理
两种方式
1 忽略json上未知的字段
设置实体类注解
@JsonIgnoreProperties(ignoreUnknown = true) public class Foo {
...
}
2 配置objectMapper
public static <T> T JsonNode2Entity(JsonNode node,Class t ,boolean throwErr) throws RuntimeException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true); //忽略大小写
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); //json 为SNAKE_CASE 实体为lowerCase Camel
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false); //对未知的属性不处理
try {
return (T) objectMapper.readValue(node.toString(), t); } catch (java.lang.Exception e) {
e.printStackTrace();
if(throwErr){
//ThrowWarnning(ErrorCode.Err_ParseParam);
throw new RuntimeException("参数解析失败");
}
return null;
}
}
例:
snake_case json
{
"context": {
"year": "2019"
},
"data": {
"GDXMZD_CZZD_GDCZLB_TITL": "项目明细",
"GDXMZD_SFWY": "0",
"GDXMZD_KFXG": "0",
"GDXMZD_LBID": "%",
"GDXMZD_XMXZ": "0",
"GDXMZD_XMMC": "新增的项目",
"GDXMZD_KFBG": "0",
"GDXMZD_GSSM": null,
"GDXMZD_QJY": "",
"CREATOR": "admin",
"LASTMODIFIER": "admin",
"GDXMZD_CZZD_GDCZLB_CZZD": "vo",
"ID": "40282e816d15be06016d15c259650000",
"GDXMZD_XMLX": "C",
"GDXMZD_KPMX": "1",
"GDXMZD_JSXS": "0",
"GDXMZD_JSGS": null,
"GDXMZD_SFBT": "0",
"GDXMZD_XSSX": 98,
"GDXMZD_CZZD": "xmmx",
"GDXMZD_DECN": 0,
"GDXMZD_XMBH": "GDZCZY_XM01",
"GDXMZD_IFENUM": "0",
"GDXMZD_LENG": "888",
"GDXMZD_IFCZ": "1",
"GDXMZD_IFPUB": "0"
}
}
转换的实体
@Data
@Entity
@DynamicInsert
@DynamicUpdate @Table(name = "figdxmzd2019")
public class GDXMZD {
@Id
@GenericGenerator(name = "uuid",strategy = "uuid")
@GeneratedValue(generator = "uuid")
@Column(name = "id", nullable = false, length = 36)
public String id;
@Column(name = "gdxmzd_lbid", nullable = false, length = 36)
public String gdxmzdLbid;
@Column(name = "gdxmzd_xmbh", nullable = false, length = 60)
public String gdxmzdXmbh;
@Column(name = "gdxmzd_xmmc", nullable = false, length = 60)
public String gdxmzdXmmc;
@Column(name = "gdxmzd_xmlx", nullable = false, length = -1)
public String gdxmzdXmlx;
@Column(name = "gdxmzd_xmxz", nullable = false, length = -1)
public String gdxmzdXmxz;
@Column(name = "gdxmzd_jsgs", length = 1000)
public String gdxmzdJsgs;
@Column(name = "gdxmzd_gssm", length = 1000)
public String gdxmzdGssm;
@Column(name = "gdxmzd_xssx", length = 3)
public int gdxmzdXssx;
@Column(name = "gdxmzd_leng", nullable = false)
public int gdxmzdLeng;
@Column(name = "gdxmzd_decn", nullable = false)
public int gdxmzdDecn;
@Column(name = "gdxmzd_ifpub", nullable = false, length = -1)
public String gdxmzdIfpub;
@Column(name = "gdxmzd_kpmx", nullable = false, length = -1)
public String gdxmzdKpmx;
@Column(name = "gdxmzd_kfbg", nullable = false, length = -1)
public String gdxmzdKfbg;
@Column(name = "gdxmzd_sfbt", nullable = false, length = -1)
public String gdxmzdSfbt;
@Column(name = "gdxmzd_kfxg", nullable = false, length = -1)
public String gdxmzdKfxg;
@Column(name = "gdxmzd_ifcz", nullable = false, length = -1)
public String gdxmzdIfcz;
@Column(name = "gdxmzd_czzd", nullable = false, length = 20)
public String gdxmzdCzzd;
@Column(name = "gdxmzd_ifenum", nullable = false, length = -1)
public String gdxmzdIfenum;
@Column(name = "gdxmzd_jsxs", nullable = false, length = -1)
public String gdxmzdJsxs;
@Column(name = "gdxmzd_sfwy", nullable = false, length = -1)
public String gdxmzdSfwy;
@Column(name = "gdxmzd_qjy", nullable = false, length = -1)
public String gdxmzdQjy;
@Column(name = "createdtime")
public LocalDateTime createdtime;
@Column(name = "lastmodifiedtime")
public LocalDateTime lastmodifiedtime;
@Column(name = "lastmodifier", length = 128)
public String lastmodifier;
@Column(name = "creator", length = 128)
public String creator; public GDXMZD(){} public GDXMZD(String xmbh){
this.gdxmzdXmbh=xmbh;
} @PrePersist
protected void onCreate() {
createdtime = CAFContext.current.getCurrentDateTime();
creator = CAFContext.current.getUserId();
} @PreUpdate
protected void onUpdate() {
lastmodifiedtime = CAFContext.current.getCurrentDateTime();
lastmodifier = CAFContext.current.getUserId();
} }
反向转换(序列化)
ObjectMapper m = new ObjectMapper();
m.setPropertyNamingStrategy(new UpperSnakCasePropertyNamingStrategy());
m.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
try {
m.writeValue(System.out, gdxmzdRepository.findAll());
} catch (IOException e) {
e.printStackTrace();
}
参考:
1 https://stackoverflow.com/questions/5455014/ignoring-new-fields-on-json-objects-using-jackson
objectMaaper 反序列化json字段多于或少于实体处理的更多相关文章
- 无实体反序列化Json
public class ExectendHelp { private int index = 0; public void GetLast(JObject obj, ref JToken token ...
- PG 中 JSON 字段的应用
13 年发现 pg 有了 json 类型,便从 oracle 转 pg,几年下来也算比较熟稔了,总结几个有益的实践. 用途一:存储设计时无法预料的文档性的数据.比如,通常可以在人员表准备一个 json ...
- C#序列化及反序列化Json对象通用类JsonHelper
当今的程序界Json大行其道.因为Json对象具有简短高效等优势,广受广大C#码农喜爱.这里发一个序列化及反序列化Json对象通用类库,希望对大家有用. public class JsonHelper ...
- C# Dynamic通用反序列化Json类型并遍历属性比较
背景 : 最近在做JAVA 3D API重写,重写的结果需要与原有的API结果进行比较,只有结果一致时才能说明接口是等价重写的,为此需要做一个API结果比较的工具,比较的内容就是Json内容,但是为了 ...
- c#实例化继承类,必须对被继承类的程序集做引用 .net core Redis分布式缓存客户端实现逻辑分析及示例demo 数据库笔记之索引和事务 centos 7下安装python 3.6笔记 你大波哥~ C#开源框架(转载) JSON C# Class Generator ---由json字符串生成C#实体类的工具
c#实例化继承类,必须对被继承类的程序集做引用 0x00 问题 类型“Model.NewModel”在未被引用的程序集中定义.必须添加对程序集“Model, Version=1.0.0.0, Cu ...
- Java基础/利用fastjson反序列化json为对象和对象数组
利用fastjson反序列化json为对象和对象数组 利用 fastjosn 将 .json文件 反序列化为 java.class 和 java.util.List fastjson 是一个性能很好的 ...
- mybatis&plus系列------Mysql的JSON字段的读取和转换
mybatis&plus系列------Mysql的JSON字段的读取和转换 一. 背景 在平常的开发中,我们可能会有这样的需求: 业务数据在存储的时候,并不是以mysql中的varchar丶 ...
- 如何用js定义数组,用js来拼接json字段
定义js数组的方式有: var arr = (); var arr = []; var arr = new Array(); 如何拼接成一个json字段. <!DOCTYPE HTML PUBL ...
- Jackson序列化和反序列化Json数据完整示例
Jackson序列化和反序列化Json数据 Web技术发展的今天,Json和XML已经成为了web数据的事实标准,然而这种格式化的数据手工解析又非常麻烦,软件工程界永远不缺少工具,每当有需求的时候就会 ...
随机推荐
- postman使用小结
需要权限验证: cookie就是存在本地的键值对,session存在服务器端的键值对 上传文件: form_data选择file ,输入传入的参数
- C# winform OpenFileDialog用法
https://jingyan.baidu.com/article/e52e36156fa6d240c60c51c8.html 详情看看这个.
- Python笔记(二十六)_魔法方法_属性的魔法方法
属性的魔法方法 __getattribute__(self,name):当该类的属性被访问时,自动触发,是最先被触发的属性方法 __setattr__(self,name,value):当一个属性被设 ...
- 【ABAP系列】SAP ABAP ALV中设置CHECKBOX同时选中事件
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP ABAP ALV中设置CHE ...
- Mysql 免安装版本配置
1. 安装命令 (制定安装目录的my.ini文件) mysqld --install MySQL --defaults-file="C:\mysql-5.7.26-winx64\bin\my ...
- jQ判断checked是否选中
(1).JQ1.6版本之前(不包括1.6版本)判断checkbox是否被选中用的是attr()方法,HTML代码与上面相同,只放JQ代码: console.log($("input[type ...
- docker--docker架构
4 docker 架构 Docker uses a client-server architecture. The Docker client talks to the Docker daemon, ...
- python基础-7.2文件内置属性__doc__ __file__ __package__ __cached__ __name__
__doc__ #py文件开头的注释文字 __file__ #当前py文件的路径 __package__ #包含文件所在包,用 . 分隔,当前文件为None,导入的其他文件:指定文件所在包,用.分隔. ...
- mybatis如何把session托管给spring
原生mybatis创建SqlSession的流程: SqlSession sqlSession = null; try { sqlSession = sqlSessionFactory.openSes ...
- 扩展欧几里得算法详解(exgcd)
一.前言 本博客适合已经学会欧几里得算法的人食用~~~ 二.扩展欧几里得算法 为了更好的理解扩展欧几里得算法,首先你要知道一个叫做贝祖定理的玄学定理: 即如果a.b是整数,那么一定存在整数x.y使得$ ...