简体   繁体   English

忽略来自 Spring 引导 API 响应的审核字段

[英]Ignore Auditing fields from Spring Boot API Response

I am searching for a better approach, that how to either exclude the auditing fields(like createdBy,createdDate) while fetching the @Entity in Data repository class or ignore them in API response.我正在寻找一种更好的方法,即如何在数据存储库 class 中获取 @Entity 时排除审计字段(如 createdBy、createdDate)或在 API 响应中忽略它们。 I know we can do it by using @Ignore or @IgnoreProperties on each property or entity class, but not interested to add it in every class, reason i have too many entities.我知道我们可以通过在每个属性或实体 class 上使用 @Ignore 或 @IgnoreProperties 来做到这一点,但没有兴趣在每个 class 中添加它,因为我有太多实体。

Is there any other approach to achieve with simple and with common implementation?有没有其他方法可以通过简单和通用的实现来实现?

If all auditing fields have the same name, Use an abstract class to manage those fields, then other entirety extend from it;如果所有的审计字段同名,使用一个抽象的class来管理这些字段,然后其他的整体从它扩展;

here is my approach:这是我的方法:

@MappedSuperclass
@EntityListeners(AuditingEntityListener::class)
abstract class ModifiedJpaEntity:Serializable {
    companion object {
        private const val serialVersionUID = -5554308939380869754L
    }
    @Column
    @CreationTimestamp
    @JsonIgnore
    var createAt:Date?=null
    @Column
    @CreatedBy
    @JsonIgnore
    var createBy:String?=null
    @Column
    @UpdateTimestamp
    @JsonIgnore
    var updateAt:Date?=null
    @Column
    @LastModifiedBy
    @JsonIgnore
    var updateBy:String?=null

    val createTimeStr: String
        get() = if(createAt!=null) SimpleDateFormat("yyyy-MM-dd HH:mm").format(createAt) else ""
    val updateTimeStr: String
        get() = if(updateAt!=null) SimpleDateFormat("yyyy-MM-dd HH:mm").format(updateAt) else ""
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM