jackson 实体转json 为NULL或者为空不参加序列化【转载】
原博客:https://www.cnblogs.com/yangy608/p/3936848.html
1.实体上
/**
* 将该标记放在属性上,如果该属性为NULL则不参与序列化
* 如果放在类上边,那对这个类的全部属性起作用
* Include.Include.ALWAYS 默认
* Include.NON_DEFAULT 属性为默认值不序列化
* Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化
* Include.NON_NULL 属性为NULL 不序列化
*/
@JsonInclude(Include.NON_NULL)
2.代码上
ObjectMapper mapper = new ObjectMapper(); /**
* 通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化
* Include.Include.ALWAYS 默认
* Include.NON_DEFAULT 属性为默认值不序列化
* Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化
* Include.NON_NULL 属性为NULL 不序列化
*/
mapper.setSerializationInclusion(Include.NON_NULL); User user = new User(1,"",null);
String outJson = mapper.writeValueAsString(user);
System.out.println(outJson);
注意:只对VO起作用,Map List不起作用,例如:
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL); Map map = new HashMap();
map.put("a", null);
map.put("b", "b"); String ret_val = mapper.writeValueAsString(map);
System.err.println(ret_val);
Map m = mapper.readValue(ret_val, Map.class);
System.err.println(m.get("a") + "|" + m.get("b"));
输出:
{"b":"b","a":null}
null|b
VO vo = new VO();
vo.setA(null);
vo.setB("b"); String ret_val1 = mapper.writeValueAsString(vo);
System.err.println(ret_val1);
VO v = mapper.readValue(ret_val1, VO.class);
System.err.println(v.getA() + "|" + v.getB());<br>
输出:
{"b":"b"}
|b

jackson 实体转json 为NULL或者为空不参加序列化【转载】的更多相关文章
- jackson 实体转json 为NULL或者为空不参加序列化
1.实体上 @JsonInclude(Include.NON_NULL) //将该标记放在属性上,如果该属性为NULL则不参与序列化 //如果放在类上边,那对这个类的全部属性起作用 //Include ...
- jackson实体转json时 为NULL不参加序列化的汇总
首先加入依赖 <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson ...
- ASP.NET Core WebApi 返回统一格式参数(Json 中 Null 替换为空字符串)
相关博文:ASP.NET Core WebApi 返回统一格式参数 业务场景: 统一返回格式参数中,如果包含 Null 值,调用方会不太好处理,需要替换为空字符串,示例: { "respon ...
- spring boot 返回json字符串 null值转空字符串
@Configuration public class JacksonConfig { @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.cl ...
- SpringMVC统一转换null值为空字符串的方法
在SpringMVC中,可以通过在<mvc:annotation-driven>中配置<mvc:message-converters>,把null值统一转换为空字符串,解决这个 ...
- SpringMVC统一转换null值为空字符串的方法 !
在SpringMVC中,可以通过在<mvc:annotation-driven>中配置<mvc:message-converters>,把null值统一转换为空字符串,解决这个 ...
- Jackson 高性能的JSON处理 ObjectMapper
http://blog.csdn.net/wangyang2698341/article/details/8223929 今天自行研究了下json ,感觉非常好用,经过测试比google的GSON快多 ...
- springboot json返回null问题处理
在开发过程中,我们需要统一返回前端json格式的数据,但有些接口的返回值存在 null或者""这种没有意义的字段.以上不仅影响理解,还浪费带宽,这时我们可以统一做一下处理:不返回空 ...
- springmvc 1.接受日期类型的参数 2.后台返回json串的格式处理(返回json串null值处理为"")
springmvc中的配置: <bean id="dateConvert" class="com.iomp.util.DateConvert"/> ...
随机推荐
- 控制warning信息在控制台的显示
在运行代码时,有时出现warning信息, 1.当你后台不需要warning信息的时候,可以直接把warning信息省略掉. 2.如果代码是循环,则会在控制台打印多次warning信息,这会使得war ...
- 001-官网安装openstack之-安装前基础环境准备
0.安装常用软件包(根据个人习惯安装需要的软件包) [root@localhost ~]# yum -y install wget vim ntp net-tools tree openssh 1.配 ...
- position:absolute 按钮左右分布:left:0 和 right:0 以及雪碧图
问题:把两个a标签按钮 垂直居中,并且分别把两个按钮放在水平左右两边顶部1,祖父元素设定:position:relative2,把.arrow 设定上下垂直居中 position:absolute; ...
- Jquery实践--精读开篇
JQuery实践,我已经看了最少三遍了.这里面的很多方法对我的工作很有帮助.但由于不是真的进行前端开发,所以JQuery中的很多功能也没有用到.所以隔一段时间想起,就会发觉,一些东西又忘记了.所以趁这 ...
- jQuery.proxy(function,context)
jQuery.proxy(function,context) 概述 jQuery 1.4 新增.返回一个新函数,并且这个函数始终保持了特定的作用域.大理石平台检定规程 当有事件处理函数要附加到元素上, ...
- PHP mysqli_connect_error() 函数
返回上一次连接错误的错误描述: <?php $con=mysqli_connect("localhost","wrong_user","my_p ...
- Ubuntu: error: snap “phpstorm” has “install-snap” change in progress
Ubuntu: error: snap “phpstorm” has “install-snap” change in progress 投稿日 : 2019-06-10 | カテゴリー : linu ...
- history API,判断页面是否是在跳转链接后返回
https://www.cnblogs.com/accordion/p/5699372.html history.replaceState(history.state, null, "htt ...
- leetcode解题报告(12):Maximum Subarray
描述 Find the contiguous subarray within an array (containing at least one number) which has the large ...
- python+opencv 图像预处理
一 python 生成随机字符串序列+ 写入到图片上 from PIL import Image,ImageDraw,ImageFont import numpy as np import rando ...