使用Jackson在数据交互时忽略特定字段

使用Jackson在数据交互时忽略特定字段

通常情况下接口响应的Json对象中并不需要create_timeupdate_timeis_deleted等字段,这时只需在实体类中的相应字段添加@JsonIgnore注解,该字段就会在序列化时被忽略。

@JsonIgnore作用就是在json序列化和反序列化时将java bean中的一些属性忽略掉

具体配置如下,详细信息可参考Jackson[官方文档]

@Data
public class BaseEntity {

    @Schema(description = "主键")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @Schema(description = "创建时间")
    @JsonIgnore
    @TableField(value = "create_time")
    private Date createTime;

    @Schema(description = "更新时间")
    @JsonIgnore
    @TableField(value = "update_time")
    private Date updateTime;

    @Schema(description = "逻辑删除")
    @JsonIgnore
    @TableField("is_deleted")
    private Byte isDeleted;
}

当然,如果你使用的是fastjson作为序列化的化,那么在需要忽略的字段上加上对应的注解来忽略该字段:

	@Schema(description = "更新时间")
    @JSONField(serialize = false)
    @TableField(value = "update_time")
    private Date updateTime;
posted @   LilyFlower  阅读(194)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示