ExtendedHierarchicalField

ExtendedHierarchicalField

本文介绍了在jackson中配置了原始父类型后,动态添加新的子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个超类Field,还有两个继承了超类Field的类.

I have one super class Field and there are two other classes that inherit the super class Field.

我想动态添加子类而不影响超类更改

I want to dynamically add subclass without affecting super class changes

public class TestConfiguration {

private List<Field> fields;


}

当字段是相同类Field的实例但没有className属性"Field"时,我想以这种方式使用映射.用于映射

I want to use the mapping in this way when fields is instance of same class Field then without className property "Field" used for mapping

{
          "fields" : [ {
            "name" : "First_name",
            "type" : {
              "fieldType" : {
                "name" : "string"
              }
            },
            "required" : true
          }]
}

当字段是子类ExtendedHierarchicalField的实例,然后className属性"ExtendedHierarchicalField"的实例时,我想以此方式使用映射.用于映射或以其他任何方式映射对象

I want to use the mapping in this way when fields is instance of child class ExtendedHierarchicalField then className property "ExtendedHierarchicalField" used for mapping or any other way for mapping the objects

{
          "fields" : [ {
            "className" : "ExtendedHierarchicalField",
            "name" : "First_name",
            "type" : {
              "fieldType" : {
                "name" : "string"
              }
            },
            "required" : true
          }]
}

推荐答案

您可以使用杰克逊注释实现相同的目的.
将您的课程定义为:
Field.java

You can achieve the same using jackson annotations.
Define your classes as:
Field.java

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, property = "className")
@JsonSubTypes({
                      @JsonSubTypes.Type(value = SubField1.class),
                      @JsonSubTypes.Type(value = SubField2.class)
              })
public class Field {
    public String name;
}

SubField1.java

public class SubField1 extends Field {
    public String subField1Property = "subField1Property value";
}

SubField2.java

public class SubField2 extends Field {
    public String subField2Property = "subField2Property value";
}

TestConfiguration.java

public class TestConfiguration {
    public List<Field> fields;
}

主要方法

public static void main(String[] args) throws JsonProcessingException {

    ObjectMapper mapper = new ObjectMapper();

    Field field = new Field();
    field.name = "main field";

    Field subField1 = new SubField1();
    subField1.name = "sub field 1";

    Field subField2 = new SubField2();
    subField2.name = "sub field 2";

    TestConfiguration testConfiguration = new TestConfiguration();
    testConfiguration.fields = Arrays.asList(field, subField1, subField2);

    String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(testConfiguration);
    System.out.println(json);
}

输出:

{
  "fields" : [ {
    "className" : ".Field",
    "name" : "main field"
  }, {
    "className" : ".SubField1",
    "name" : "sub field 1",
    "subField1Property" : "subField1Property value"
  }, {
    "className" : ".SubField2",
    "name" : "sub field 2",
    "subField2Property" : "subField2Property value"
  } ]
}

注意:

  • className属性是强制性的(即使对于顶级类Field也是如此),原因是当您将相同的json反序列化为没有className属性的POJO时,它将混淆哪个实例创建FieldSubField1SubField2.
  • 为方便起见,我在POJO中使用了公共财产.您应该只喜欢使用setter/getter的私有字段.
  • className property is kind of mandatory (even for top level class Field) due to the fact that when you deserialize the same json back to POJO without className property, it will get confused which instance to create, Field, SubField1 or SubField2.
  • I used public properties in POJO for ease. You should prefer private fields with setter / getter only.

这篇关于在jackson中配置了原始父类型后,动态添加新的子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 05:02