Spring json忽略空值

    最近在处理开发的时候需要一个问题,ResponseBody的返回结果包含第三方的对象,然后这个对象包含很多的属性都为null值,然后返回到前端,整个返回对象非常大;这时候需要在返回的时候忽略这些null属性;

SpringMvc的rest接口一般使用json格式作为返回结果,通过HttpMessageConverter将我们的dto对象转换为json格式返回到前端;一般来说忽略null属性有几个办法:

1.在实体上使用@JsonInclude(JsonInclude.Include.NON_NULL)来标注

public class GitHubUser {
    private String name;
    private String blog;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getBlog() {
        return blog;
    }
    public void setBlog(String blog) {
        this.blog = blog;
    }
    
    public static void main(String[] args) throws JsonProcessingException {
        GitHubUser user = new GitHubUser();
        user.setBlog("ssss");
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writeValueAsString(user));
    }
}
返回结果:{"name":null,"blog":"ssss"}
 
  

然后我们在类上面添加

Spring json忽略空值_第1张图片

返回的json就会忽略掉null值,该注解标注在对象上会对对象的所有属性起作用,标注在某个属性上,则只对某个属性起作用;另外需要注意的是,如果当前的某个属性是对象,则子对象的null属性仍然会显示出来,所以嵌套的对象时候,这个比较麻烦;例如

    
package com.example.springbootDemo.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Created by wangdi on 2017/8/17.
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class GitHubUser {
    private String name;
    private String blog;
    private Greeting greeting;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getBlog() {
        return blog;
    }
    public void setBlog(String blog) {
        this.blog = blog;
    }

    public Greeting getGreeting() {
        return greeting;
    }

    public void setGreeting(Greeting greeting) {
        this.greeting = greeting;
    }

    public static void main(String[] args) throws JsonProcessingException {
        GitHubUser user = new GitHubUser();
        user.setBlog("ssss");
        Greeting greet = new Greeting();
        greet.setAbcd("aaaaaa");
        user.setGreeting(greet);
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writeValueAsString(user));
    }
}
其中Greeting类又包含两个属性,如图所示
public class Greeting {

    private String content;

    private String abcd;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getAbcd() {
        return abcd;
    }

    public void setAbcd(String abcd) {
        this.abcd = abcd;
    }

    public Greeting(String content) {
        this.content = content;
    }
    public Greeting() {
    }
}

测试main方法如下

public static void main(String[] args) throws JsonProcessingException {
    GitHubUser user = new GitHubUser();
    user.setBlog("ssss");
    Greeting greet = new Greeting();
    greet.setAbcd("aaaaaa");
    user.setGreeting(greet);
    ObjectMapper mapper = new ObjectMapper();
    System.out.println(mapper.writeValueAsString(user));
}
返回结果{"blog":"ssss","greeting":{"content":null,"abcd":"aaaaaa"}}

2.全局性的设置,springboot提供了配置的解决方法,在application.property中添加


jackson:
        serialization.write_dates_as_timestamps: false
        default-property-inclusion: non_null

即可,包括上面的嵌套的情况依然可以解决;

3.在具体序列化的时候设置,可以解决嵌套的情况;

Spring json忽略空值_第2张图片

个人偏爱第二种

你可能感兴趣的:(基础知识,springboot)