我如何拥有多个JsonView继承

问题描述 投票:0回答:1

我在Java Spring Boot 1.5.7中有一个场景,我有多个类,我希望能够通过继承等方式为每个类应用不同的JsonView。

 ClassA{

    @JsonView({SummaryView.class})
    public String name;

    @JsonView({DetailView.class})
    public String description;

    public interface SummaryView{}

    public interface DetailView extends SummaryView {}
 }

A类是B类中的属性。当使用ClassB的DetailView JsonView时,ClassB只应序列化为Json。

 ClassB{

    @JsonView({SummaryView.class})
    private String name;

    @JsonView({DetailView.class})
    private String description;

    @JsonView({DetailView.class})
    private ClassA classA;

    public interface SummaryView{}

    public interface DetailView extends SummaryView, ClassA.SummaryView {}
 }

B类也是C类的财产。

 ClassC{

    @JsonView({SummaryView.class})
    private String name;

    @JsonView({DetailView.class})
    private String description;

    @JsonView({DetailView.class})
    private ClassB classB;

    public interface SummaryView{}

    public interface DetailView extends SummaryView, ClassB.DetailView {}
 }

在我的现实生活中,我可以拥有最多5级的继承。在我的休息控制器中,我想使用不同的视图。当我使用ClassC.DetailView.class视图时,JsonView只是使用直接类,即ClassC和ClassB,但ClassA属性不包含在生成的Json中。结果是:

{
  "name": "classC",
  "description": "Has submodel Class B",

  "classB": {
    "name": "classB",
    "description": "Has submodel ClassA",

    "classA": { }
  }
}

我还尝试将JsonView类放在最里面的类中最外层的类中。在ClassA属性中使用@JsonView(ClassC.DetailView.class)但它也无法正常工作。

我想要的结果是:

{
  "name": "classC",
  "description": "Has submodel Class B",

  "classB": {
    "name": "classB",
    "description": "Has submodel ClassA",

    "classA": {
      "name": "classA"
    }
  }
}

我该如何工作?

json spring-mvc spring-boot jackson json-view
1个回答
0
投票

我知道以下不是答案。但是,我复制了您的类和视图层次结构。当我用一个给定的视图明确地调用Jackson的ObjectMapper时,我得到了预期的结果(使用最新的Jackson版本,2.9.3)

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;

public class TestJsonViewInheritence
{

    public static void main(String[] args) {
        try {
        ObjectMapper mapper = new ObjectMapper();
        mapper
          .enable(SerializationFeature.INDENT_OUTPUT)
          .writerWithView(ClassC.DetailView.class)
          .writeValue(System.out, new ClassC());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class ClassA
    {
        @JsonView({ SummaryView.class })
        public String name = "ClassA";

        @JsonView({ DetailView.class })
        public String description = "Has no submodel";

        public interface SummaryView {}

        public interface DetailView extends SummaryView {}
    }

    public static class ClassB
    {
        @JsonView({ SummaryView.class })
        private String name = "ClassB";

        @JsonView({ DetailView.class })
        private String description = "Has submodel ClassA";

        @JsonView({ DetailView.class })
        private ClassA classA = new ClassA();

        public interface SummaryView {}

        public interface DetailView extends SummaryView, ClassA.SummaryView {}
    }

    public static class ClassC
    {
        @JsonView({ SummaryView.class })
        private String name = "ClassC";

        @JsonView({ DetailView.class })
        private String description = "Has submodel ClassB";

        @JsonView({ DetailView.class })
        private ClassB classB = new ClassB();

        public interface SummaryView {}

        public interface DetailView extends SummaryView, ClassB.DetailView {}
    }
}

输出:

{
  "name" : "ClassC",
  "description" : "Has submodel ClassB",
  "classB" : {
    "name" : "ClassB",
    "description" : "Has submodel ClassA",
    "classA" : {
      "name" : "ClassA"
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.