How to serialize using @Jsonview with nested objects

前端 未结 3 1378
Happy的楠姐
Happy的楠姐 2021-01-04 08:49

I have a class which holds a collection of another class.

class A{
 @JsonView(VerboseViewA.Minimal.class)
 String field1;
 @JsonView(VerboseViewA.Complete.c         


        
3条回答
  •  梦毁少年i
    2021-01-04 09:16

    you need to change your json view hierachy a little:

    public class MinimalView { };
    public class AComplete extends MinimalView { };
    public class BComplete extends MinimalView { };
    

    then you can annotate your classes like

    class A{
        @JsonView(MinimalView.class)
        String field1;
        @JsonView(AComplete.class)
        String field2;
        @JsonView(AComplete.class)
        Collection bEntities;
     }
    
    class B{
        @JsonView(Minimal.class)
        String field2;
        @JsonView(BComplete.class)
        String field3;
    }
    

    when you serialize using the view AComplete, the serializer will take the AComplete and MinimalView properties because AComplete extends MinimalView.

提交回复
热议问题