赞
踩
java实体类值为null或者无关紧要的属性,也返回给了前端,看起来不太高明。可以使用JsonInclude注解或者JsonField注解过滤掉。
用在实体类前或者属性前都可以。
@JsonInclude(value= JsonInclude.Include.NON_NULL)
用在属性前。
@JSONField(serialize = false)
import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; import lombok.Getter; import com.fasterxml.jackson.annotation.JsonInclude; import java.io.Serializable; /** * * @author yangyile * @since 2023-03-18 */ @AllArgsConstructor @NoArgsConstructor @Getter @JsonInclude(value = JsonInclude.Include.NON_NULL) public class ResultInfo implements Serializable { private Object data; private Integer code; private String msg; private Integer total; public ResultInfo(Object data, Integer code, String msg) { this.data = data; this.code = code; this.msg = msg; } public static ResultInfo success(Object data) { return new ResultInfo(data, ResultCode.SUCCESS.getCode(), "success"); } public static ResultInfo success(Object data, Integer total) { return new ResultInfo(data, ResultCode.SUCCESS.getCode(), "success", total); } }
以上示例中,当total为null时不返回,适用于没有分页的情况。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。