zoukankan      html  css  js  c++  java
  • @JsonInclude(Include.NON_NULL)

    项目框架:spring+springMVC+mybatis

    @JsonInclude(Include.NON_NULL)的作用:jackson 实体转json 为NULL的字段不参加序列化(即不显示)

    实体类:

    package com.baidu.entity;
    
    import com.fasterxml.jackson.annotation.JsonIgnore;
    import com.fasterxml.jackson.annotation.JsonInclude;
    import org.springframework.data.annotation.Transient;
    
    import java.io.Serializable;
    
    public class User implements Serializable{
        private static final long serialVersionUID = 8121761080892505330L;
        private String username;
        /*@Transient
        @JsonIgnore*/
        @JsonInclude(JsonInclude.Include.NON_NULL)
        private String password; //transient
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }

    后台返回结果:

    页面返回结果:

    从图片上可以看出后台返回的实体在转化为json时,字段值为null的字段不显示。

  • 相关阅读:
    mysql基于Altas读写分离并实现高可用
    mysql基于GTIDS复制
    mysql创建用户账号出错
    mysql存储引擎
    mysql读写分离
    for each ;for in;for of 三者的区别
    关于编程的历史
    用indexof来统计字符出现的次数
    正则表达式
    DOM,BOM
  • 原文地址:https://www.cnblogs.com/super-chao/p/8484490.html
Copyright © 2011-2022 走看看