使用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 @ 2024-07-08 16:18  LilyFlower  阅读(2)  评论(0编辑  收藏  举报