java實(shí)體類值為null或者無(wú)關(guān)緊要的屬性,也返回給了前端,看起來(lái)不太高明??梢允褂肑sonInclude注解或者JsonField注解過濾掉。
JsonInclude注解
用在實(shí)體類前或者屬性前都可以。
@JsonInclude(value= JsonInclude.Include.NON_NULL)
JsonField注解
用在屬性前。
@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);
}
}
以上示例中,當(dāng)total為null時(shí)不返回,適用于沒有分頁(yè)的情況。
加JsonInclude注解前
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-521981.html
加JsonInclude注解后
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-521981.html
到了這里,關(guān)于java實(shí)體類屬性值為null不返回或者某個(gè)屬性不返回的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!