zoukankan      html  css  js  c++  java
  • jackson 实体转json 为NULL或者为空不参加序列化

    package com.sarrs;
    
    import ch.qos.logback.core.net.SyslogOutputStream;
    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    /**
     * Created by hubo7 on 2018/8/20.
     * test for : https://www.cnblogs.com/yangy608/p/3936848.html
     */
    public class jacksonTest {
        private static ObjectMapper mapper = new ObjectMapper();
        public static void main(String[] args){
            Student stu = new Student("小明",112,"男");
            String stuStr = null;
            JSONObject stuJSONObject = null;
            try{
                //mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
                stuStr = mapper.writeValueAsString(stu);
                stuJSONObject = JSON.parseObject(stuStr);
            }catch(Exception e){
                System.out.print(e.getMessage());
            }
            System.out.println(stuStr);
            System.out.println(stuJSONObject);
            System.out.println(stuJSONObject.toJSONString());
        }
    }
    
    @JsonInclude(JsonInclude.Include.NON_NULL)
    class Student {
        private String name;
        private Integer id;
        private String sex;
        private String birthday;
        private String clazz;
    
        public Student() {
        }
    
        public Student(String name, Integer id, String sex) {
            this.name = name;
            this.id = id;
            this.sex = sex;
        }
    
        public String getName() {
            return name;
        }
    
        public Integer getId() {
            return id;
        }
    
        public String getSex() {
            return sex;
        }
    
        public String getBirthday() {
            return birthday;
        }
    
        public String getClazz() {
            return clazz;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        public void setBirthday(String birthday) {
            this.birthday = birthday;
        }
    
        public void setClazz(String clazz) {
            this.clazz = clazz;
        }
    }
  • 相关阅读:
    学习GlusterFS(二)
    并不对劲的莫比乌斯反演
    并不对劲的WC2019
    并不对劲的loj2134:uoj132:p2304:[NOI2015]小园丁与老司机
    并不对劲的bzoj3626:loj2558:p4211:[LNOI2014]LCA
    并不对劲的bzoj3214:p3333:[ZJOI2013]丽洁体
    并不对劲的bzoj4827:loj2020:p3723:[AHOI/HNOI2017]礼物
    并不对劲的bzoj4560:p3269:[JLOI2016]字符串覆盖
    并不对劲的p2664树上游戏
    并不对劲的p3709:大爷的字符串题
  • 原文地址:https://www.cnblogs.com/Allen-win/p/9510218.html
Copyright © 2011-2022 走看看