Spring自帶Jackson序列化json去除值為null的字段


1. @JsonInclude

在類或者字段上加注解@JsonInclude(JsonInclude.Include.NON_NULL),Spring請求返回對象時就不會包含值為null的字段

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BaseQuery {

    private Integer uid;
    private Integer page;
    private String num;
}

2. ObjectMapper

可以使用ObjectMapper.writeValueAsString()來處理

ObjectMapper objectMapper = new ObjectMapper();
String value = objectMapper.writeValueAsString(query);
System.out.println(value);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM