Override @JsonIgnore annotation for some controllers. Jackson Spring

I need to serialize the photo box only for direct queries to the entity. But when the user requests all MediaHolders objects, serialize only the short data using photoSize.

I use Kotlin, Jackson, Spring and Hibernate. I tried:

  • @JsonView on contoller. Does not work. JsonView does not override @JsonIgnore behavior. Or I will need to annotate all the fields in the base class of PersonalDomainObject and all controllers, which is not suitable for me.
  • Custom JsonFilter, but the same thing, it does not see the ignore field. And I can not add different filters on different controllers.

Maybe I missed something, but I think this is a very common optimization task. Thanks so much for any suggestion!

  @JsonIgnoreProperties("createdAt", "updatedAt", "owner", "hibernateLazyInitializer", "handler")
    open class MediaHolder : PersonalDomainObject() {

        @OneToMany(cascade = arrayOf(CascadeType.PERSIST, CascadeType.REMOVE), mappedBy = "mediaHolder")
        @LazyCollection(LazyCollectionOption.EXTRA)
        var photos:MutableList<Photo> = mutableListOf()

        val photosSize: Int
            get() = photos.size
}
+4

Source: https://habr.com/ru/post/1657377/


All Articles