jackSon注解-- @JsonInclude 注解不返回null值字段

2024-03-19 13:38

本文主要是介绍jackSon注解-- @JsonInclude 注解不返回null值字段,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

jackSon注解– @JsonInclude 注解不返回null值字段
Spring Boot项目中遇到的小知识

@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;


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@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配置
1
2
3
4
5
6
7
8
9
10
JSON库 Jackson 常用注解介绍
注:以下所涉及到的实体类都使用了 Lomback 插件

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;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
使用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);

    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
访问: localhost/sell/jackson/test1

使用注解前:返回值

{
    "code": 0,
    "msg": "成功",
    "data": {
        "id": "1",
        "username": "user1",
        "password": "123456",
        "openid": "openid",
        "createTime": null,
        "updateTime": null
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
使用注解后:返回值

{
    "code": 0,
    "msg": "成功",
    "data": {
        "id": "1",
        "username": "user1",
        "password": "123456",
        "openid": "openid",
    }
}
1
2
3
4
5
6
7
8
9
10
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;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
public class SomeEntity {
    private String name;
    private String desc;
    private SomeOtherEntity entity;
}
1
2
3
4
5
SomeEntity 中的 entity 属性在json处理时会被忽略。

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

public class SomeEntity {
    @JsonProperty("user_name")
    private String userName;
      // ...
}
1
2
3
4
5
二、只在序列化情况下生效的注解
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;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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);

    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
结果:

{
    "code": 0,
    "msg": "成功",
    "data": {
        "id": "1",
        "username": "user1",
        "openid": "openid"
    }
}
1
2
3
4
5
6
7
8
9
10
上述例子的意思是 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;
    }
}
1
2
3
4
5
6
7
上述例子中在将 json 字符串转换成 SomeEntity 实例时,会将 json 字符串中的 description 字段赋值给 SomeEntity 的 desc 属性。
--------------------- 
原文:https://blog.csdn.net/itguangit/article/details/78701110 

这篇关于jackSon注解-- @JsonInclude 注解不返回null值字段的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/826171

相关文章

mapstruct中的@Mapper注解的基本用法

《mapstruct中的@Mapper注解的基本用法》在MapStruct中,@Mapper注解是核心注解之一,用于标记一个接口或抽象类为MapStruct的映射器(Mapper),本文给大家介绍ma... 目录1. 基本用法2. 常用属性3. 高级用法4. 注意事项5. 总结6. 编译异常处理在MapSt

统一返回JsonResult踩坑的记录

《统一返回JsonResult踩坑的记录》:本文主要介绍统一返回JsonResult踩坑的记录,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录统一返回jsonResult踩坑定义了一个统一返回类在使用时,JsonResult没有get/set方法时响应总结统一返回

MyBatis设计SQL返回布尔值(Boolean)的常见方法

《MyBatis设计SQL返回布尔值(Boolean)的常见方法》这篇文章主要为大家详细介绍了MyBatis设计SQL返回布尔值(Boolean)的几种常见方法,文中的示例代码讲解详细,感兴趣的小伙伴... 目录方案一:使用COUNT查询存在性(推荐)方案二:条件表达式直接返回布尔方案三:存在性检查(EXI

Spring @RequestMapping 注解及使用技巧详解

《Spring@RequestMapping注解及使用技巧详解》@RequestMapping是SpringMVC中定义请求映射规则的核心注解,用于将HTTP请求映射到Controller处理方法... 目录一、核心作用二、关键参数说明三、快捷组合注解四、动态路径参数(@PathVariable)五、匹配请

SpringCloud中的@FeignClient注解使用详解

《SpringCloud中的@FeignClient注解使用详解》在SpringCloud中使用Feign进行服务间的调用时,通常会使用@FeignClient注解来标记Feign客户端接口,这篇文章... 在Spring Cloud中使用Feign进行服务间的调用时,通常会使用@FeignClient注解

Python函数返回多个值的多种方法小结

《Python函数返回多个值的多种方法小结》在Python中,函数通常用于封装一段代码,使其可以重复调用,有时,我们希望一个函数能够返回多个值,Python提供了几种不同的方法来实现这一点,需要的朋友... 目录一、使用元组(Tuple):二、使用列表(list)三、使用字典(Dictionary)四、 使

Java 关键字transient与注解@Transient的区别用途解析

《Java关键字transient与注解@Transient的区别用途解析》在Java中,transient是一个关键字,用于声明一个字段不会被序列化,这篇文章给大家介绍了Java关键字transi... 在Java中,transient 是一个关键字,用于声明一个字段不会被序列化。当一个对象被序列化时,被

Spring Cache注解@Cacheable的九个属性详解

《SpringCache注解@Cacheable的九个属性详解》在@Cacheable注解的使用中,共有9个属性供我们来使用,这9个属性分别是:value、cacheNames、key、key... 目录1.value/cacheNames 属性2.key属性3.keyGeneratjavascriptor

使用@Cacheable注解Redis时Redis宕机或其他原因连不上继续调用原方法的解决方案

《使用@Cacheable注解Redis时Redis宕机或其他原因连不上继续调用原方法的解决方案》在SpringBoot应用中,我们经常使用​​@Cacheable​​注解来缓存数据,以提高应用的性能... 目录@Cacheable注解Redis时,Redis宕机或其他原因连不上,继续调用原方法的解决方案1

Spring Boot 常用注解整理(最全收藏版)

《SpringBoot常用注解整理(最全收藏版)》本文系统整理了常用的Spring/SpringBoot注解,按照功能分类进行介绍,每个注解都会涵盖其含义、提供来源、应用场景以及代码示例,帮助开发... 目录Spring & Spring Boot 常用注解整理一、Spring Boot 核心注解二、Spr