@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class OrderDTO { private String orderId;
@JsonProperty("name")
private String buyerName;
@JsonProperty("phone")
private String buyerPhone;
@JsonProperty("address")
private String buyerAddress;
@JsonProperty("openid")
private String buyerOpenid;
private BigDecimal orderAmount; /**
* 订单状态,默认是0
*/
private Integer orderStatus; /**
* 支付状态
*/
private Integer payStatus; @JsonSerialize(using = Date2LongSerializer.class)
private Timestamp createTime;
@JsonSerialize(using = Date2LongSerializer.class)
private Timestamp updateTime; @JsonProperty("items")
List<OrderDetailEntity> orderDetailList; }

@JsonInclude(JsonInclude.Include.NON_NULL)表示,如果值为null,则不返回

全局jsckson配置

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://192.168.41.60/sell?characterEncoding=utf-8&useSSL=false
jpa:
show-sql: true
jackson:
default-property-inclusion: non_null # 全局jackson配置

JSON库 Jackson 常用注解介绍

Jackson JSON 框架中包含了大量的注解来让我们可以干预 Jackson 的 JSON 处理过程,
例如我们可以通过注解指定 java pojo 的某些属性在生成 json 时被忽略。。本文主要介绍如何使用 Jackson 提供的注解。
Jackson注解主要分成三类,一是只在序列化时生效的注解;二是只在反序列化时候生效的注解;三是两种情况下都生效的注解。

一: 两种情况下都有效的注解

1. @JsonIgnore 作用域属性或方法上

@JsonIgnore 用来告诉 Jackson 在处理时忽略该注解标注的 java pojo 属性,
不管是将 java 对象转换成 json 字符串,还是将 json 字符串转换成 java 对象。

@Data
public class SellerInfoEntity { private String id;
private String username;
private String password;
private String openid; @JsonIgnore
private Timestamp createTime;
@JsonIgnore
private Timestamp updateTime; public SellerInfoEntity() {
} public SellerInfoEntity(String id, String username, String password, String openid) {
this.id = id;
this.username = username;
this.password = password;
this.openid = openid;
}
}

2. @JsonIgnoreProperties 作用在类上

@JsonIgnoreProperties 和 @JsonIgnore 的作用相同,都是告诉 Jackson 该忽略哪些属性,
不同之处是 @JsonIgnoreProperties 是类级别的,并且可以同时指定多个属性。

@Data
@JsonIgnoreProperties(value = {"createTime","updateTime"})
public class SellerInfoEntity { private String id;
private String username;
private String password;
private String openid; private Timestamp createTime;
private Timestamp updateTime; public SellerInfoEntity() {
} public SellerInfoEntity(String id, String username, String password, String openid) {
this.id = id;
this.username = username;
this.password = password;
this.openid = openid;
}
}

使用Spring Boot快速搭建Controller进行测试:

@RestController
@RequestMapping("/jackson")
public class TestJackson {
@RequestMapping("test1")
public Result test1(){ SellerInfoEntity entity = new SellerInfoEntity("1","user1","123456","openid"); return new Result(MyResultEnum.SUCCESS,entity); }
}

访问: localhost/sell/jackson/test1

使用注解前:返回值

{
"code": 0,
"msg": "成功",
"data": {
"id": "1",
"username": "user1",
"password": "123456",
"openid": "openid",
"createTime": null,
"updateTime": null
}
}

使用注解后:返回值

{
"code": 0,
"msg": "成功",
"data": {
"id": "1",
"username": "user1",
"password": "123456",
"openid": "openid",
}
}

3. @JsonIgnoreType

@JsonIgnoreType 标注在类上,当其他类有该类作为属性时,该属性将被忽略。

package org.lifw.jackosn.annotation;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
@JsonIgnoreType
public class SomeOtherEntity {
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
public class SomeEntity {
private String name;
private String desc;
private SomeOtherEntity entity;
}

SomeEntity 中的 entity 属性在json处理时会被忽略。

4. @JsonProperty

@JsonProperty 可以指定某个属性和json映射的名称。例如我们有个json字符串为{“user_name”:”aaa”},
而java中命名要遵循驼峰规则,则为userName,这时通过@JsonProperty 注解来指定两者的映射规则即可。这个注解也比较常用。

public class SomeEntity {
@JsonProperty("user_name")
private String userName;
// ...
}

二、只在序列化情况下生效的注解

1. @JsonPropertyOrder

在将 java pojo 对象序列化成为 json 字符串时,使用 @JsonPropertyOrder 可以指定属性在 json 字符串中的顺序。

2. @JsonInclude

在将 java pojo 对象序列化成为 json 字符串时,使用 @JsonInclude 注解可以控制在哪些情况下才将被注解的属性转换成 json,例如只有属性不为 null 时。

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class SellerInfoEntity { private String id;
private String username; @JsonInclude(JsonInclude.Include.NON_EMPTY)
private String password;
private String openid; private Timestamp createTime;
private Timestamp updateTime; public SellerInfoEntity() {
} public SellerInfoEntity(String id, String username, String password, String openid) {
this.id = id;
this.username = username;
this.password = password;
this.openid = openid;
}
}

Controller 测试

@RestController
@RequestMapping("/jackson")
public class TestJackson { @RequestMapping("test1")
public Result test1(){ SellerInfoEntity entity = new SellerInfoEntity("1","user1","","openid"); return new Result(MyResultEnum.SUCCESS,entity);
}
}

结果:

{
"code": 0,
"msg": "成功",
"data": {
"id": "1",
"username": "user1",
"openid": "openid"
}
}

上述例子的意思是 SellerInfoEntity 的所有属性只有在不为 null 的时候才被转换成 json,
如果为 null 就被忽略。并且如果password为空字符串也不会被转换.

该注解也可以加在某个字段上。

另外还有很多其它的范围,例如 NON_EMPTY、NON_DEFAULT等

三、是在反序列化情况下生效的注解

1. @JsonSetter

@JsonSetter 标注于 setter 方法上,类似 @JsonProperty ,也可以解决 json 键名称和 java pojo 字段名称不匹配的问题。

public class SomeEntity {
private String desc;
@JsonSetter("description")
public void setDesc(String desc) {
this.desc = desc;
}
}

上述例子中在将 json 字符串转换成 SomeEntity 实例时,会将 json 字符串中的 description 字段赋值给 SomeEntity 的 desc 属性。

jackSon注解– @JsonInclude 注解不返回null值字段的更多相关文章

  1. copyProperties 忽略null值字段

    在做项目时遇到需要copy两个对象之间的属性值,但是有源对象有null值,在使用BeanUtils来copy时null值会覆盖目标对象的同名字段属性值,然后采用以下方法找到null值字段,然后忽略: ...

  2. spring配置jackson不返回null值

    #json不返回null spring.jackson.default-property-inclusion=non_null

  3. Flash的坑之ExternalInterface.call只返回null值的解决办法

    flash坑太多了,要确保能有效的使用ExternalInterface.call调用js的话,需要两个条件: 1.allowScriptAccess="always" 2.id= ...

  4. select sum也会返回null值

    SELECT  SUM(detail.VAL)  FROM   AI_SDP_ORDER_MONTH_DETAIL_201706    detail 如果所有的VAL都是null的话,或者根本就不存在 ...

  5. 使用MyBatis查询int类型字段,返回NULL值时报异常的解决方法

    当配置mybatis返回int类型时 select id="getUserIdByName" parameterType="string" resultType ...

  6. @JsonInclude注解,RestTemplate传输值为null的属性,利用FastJson将属性中有空值null的对象转化成Json字符串

    一个pojo类: import lombok.Data; @Data public class Friend { private String name; private int age; priva ...

  7. 如何让access空值变成0?(确切的说是让access Null值变成0)

    方法一 if  IsNull(Me.新_退休费) = True Then Me.新_退休费 = 0 方法二 if Nz(Me.原_退休费) = Me.原_退休费 Then Me.原_退休费 = 0 有 ...

  8. TSQL 聚合函数忽略NULL值

    max,min,sum,avg聚合函数会忽略null值,但不代表聚合函数不返回null值,如果表为空表,或聚合列都是null,则返回null.count 聚合函数忽略null值,如果聚合列都是null ...

  9. null值与空值比较

    JAVA中判断字符串或者数值是否为空时,常用到  .equals函数对空值进行判断 例如  values[5]为参数值 "".equals(values[5]) 常在if语句判断中 ...

随机推荐

  1. Django中ORM简介与单表数据操作

    一. ORM简介  概念:.ORM框架是用于实现面向对象编程语言种不同类型系统的数据之间的转换 构建模型的步骤:重点 (1).配置目标数据库信息,在seting.py中设置数据库信息 DATABASE ...

  2. 力扣(LeetCode) 136. 只出现一次的数字

    给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: 输入: [ ...

  3. 在浏览器端用es6,babel+browserify打包

    写得最清楚的是这个系列: 一个普通的写网页的人如何过渡到ES6 (一) 感觉比babel官网写得还清楚点. 看完这个才有点理解node原来不只是用来起express后端web server,更主要用途 ...

  4. WebStorm Error : program path not specified

    1.出现这个错误是由于没有设置Node.js路径引起的. 2.下载安装Node.js. 3.设置对应的路径,设置后点一下Enable按钮即可. 以上,完.

  5. Pandas存储为Excel格式:单个xlsx文件下多sheet存储方法

    Notes If passing an existing ExcelWriter object, then the sheet will be added to the existing workbo ...

  6. 第 4 章 容器 - 029 - 限制容器的 Block IO

    限制容器的 Block IO Block IO 是另一种可以限制容器使用的资源. Block IO 指的是磁盘的读写,docker 可通过设置权重.限制 bps 和 iops 的方式控制容器读写磁盘的 ...

  7. 关于ORA-00979 不是 GROUP BY 表达式错误的解释

    ORA-00979 不是 GROUP BY 表达式”这个错误,和我前面介绍的另外一个错误ORA-00937一样使很多初学oracle的人爱犯的. 我在介绍使用聚合函数中用group by来分组数据时特 ...

  8. LeetCode--283--移动0

    问题描述: 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原 ...

  9. hdoj5785

    题意:略 先用题解的办法,manacher,然后tag,add数组.但是比较难办的是manacher加了新的字符.这样的话cntL和cntR不是实际的值,但是没关系,原本的字符都在奇数位置,这样cnt ...

  10. Android Studio 一直卡在building解决办法

    1.随便找一个你能运行的as项目 2.打开gradle-wrapper.properties,文件目录:项目/gradle/wrapper/gradle-wrapper.properties 3.复制 ...