首页 > 解决方案 > Spring数据休眠根据方法序列化项目属性

问题描述

我有个问题。让我们想象一下,我们有一个 Hotel 实体。该实体包含例如id、name、Country 对象引用、主题照片路径和其他照片补丁列表

我正在使用 Spring Boot JPA + jackson。所以存储库就像...

@Repository
public interface HotelRepository extends CrudRepository<HotelEntity, Long> {
    List<HotelEntity> findTop3ByCountryOrderByPriorityDesc(CountryEntity country);
}

有没有可能用一些杰克逊注释实体字段来告诉 Spring boot-“嘿,如果你使用这个存储库方法(例如 findTop3ByCountryOrderByPriorityDesc 方法),只取名称、id 和主题照片。(这种方法将用于所有实体和方法)。像

@JsonSerializeClasses({"top3Hotels", "hotels"})    
String name;

以及在 repositorz 中的使用

@JsonSerializeClassesUsage({top3Hotels})
List<HotelEntity> findTop3ByCountryOrderByPriorityDesc(CountryEntitycountry);

(此方法将返回仅包含名称的酒店实体列表)。

注意:它也可以以“消极”的方式工作。因此,如果我用一些“类中的类”注释某个字段,该字段将不会包含在方法的返回对象中,它的注释中没有包含这个“内部类”。

这在杰克逊的 Spring 数据中是否可行?

或者我真的必须返回对象,如果我不想要某些属性,我必须迭代它们并手动“null”它?

谢谢你。

标签: springhibernatejacksonspring-data-jpaspring-data

解决方案


推荐阅读