为jackson添加动态json属性作为java pojo

为jackson添加动态json属性作为java pojo,java,jackson,Java,Jackson,我需要从Java pojo创建一个Json负载,如下所示: { "update" : { "labels" : [{"add" : "A label"} ] } } 所以我创建了一个Java pojo,如下所示: @JsonRootName(value = "update") public class AddLabel{ @JsonProperty("labels") public List<Label> labels; public AddLabe

我需要从Java pojo创建一个Json负载,如下所示:

{ "update" : {
    "labels" : [{"add" : "A label"} ]
    }
 }
所以我创建了一个Java pojo,如下所示:

@JsonRootName(value = "update")
public class AddLabel{
  @JsonProperty("labels")
  public List<Label> labels; 
  public AddLabel(String labelName){
     this.labels = new ArrayList<>(); 
     this.labels.add(new Label(labelName); 
  }
  public static class Label{
    public String add; 
    public Label(String labelName){
       this.add = labelName; 
    }
  }
}

我希望标签类中的add属性是动态的,这样它也可以接受诸如remove、set之类的值。除了为每个人创建另一个POJO之外,还有其他方法可以做到这一点吗

您的规范不清楚,您可能需要添加、删除许多相同的操作,或者最多一个

第一选择 如果同一操作最多只有一个实例,则可以使用常规对象替换数组:

{ "update" : {
    "labels" : {
       "add" : "A label",
       "remove" : "B label"
      }
    }
 }
然后将标签更改为地图而不是列表

第二种选择 如果同一动作有多个实例,可以更改标签的设计,使其包含两个字段:值和动作

并相应地更新标签:

公共静态类标签{ 公共字符串操作; 公共字符串值 公共标签字符串操作,字符串值{ 这个动作=动作; 这个值=值; } } 您还可以将操作更改为枚举,枚举定义所有可能的操作

第三种选择 如果您真的需要坚持json设计,那么您没有很多选项来定义动态json的静态类型类。也许可以用jackons的多态性来破解这个问题。但是,您可以始终只使用json对象,并将标签保留为列表。您可以使用允许创建动态键值对的注释。下面的示例显示了如何为同一标签类生成3个不同的键:

另见:

{ "update" : {
    "labels" : [{"action": "add", "value" : "A label"} ]
    }
 }
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class JsonApp {

    public static void main(String[] args) throws Exception {
        AddLabel label = new AddLabel("A label");
        label.getLabels().add(AddLabel.Label.remove("Remove"));
        label.getLabels().add(AddLabel.Label.set("Set"));

        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        System.out.println(mapper.writeValueAsString(label));
    }
}

@JsonRootName(value = "update")
class AddLabel {

    @JsonProperty("labels")
    private List<Label> labels;

    public AddLabel(String labelName) {
        this.labels = new ArrayList<>();
        this.labels.add(Label.add(labelName));
    }

    public List<Label> getLabels() {
        return labels;
    }

    public static class Label {

        private final String key;
        private final String value;

        private Label(String key, String value) {
            this.key = key;
            this.value = value;
        }

        public static Label add(String value) {
            return new Label("add", value);
        }

        public static Label remove(String value) {
            return new Label("remove", value);
        }

        public static Label set(String value) {
            return new Label("set", value);
        }

        @JsonAnyGetter
        public Map<String, String> getDynamic() {
            return Collections.singletonMap(key, value);
        }
    }
}
{
  "labels" : [ {
    "add" : "A label"
  }, {
    "remove" : "Remove"
  }, {
    "set" : "Set"
  } ]
}