简体   繁体   English

如何在JAX-RS中映射动态JSON

[英]How to map dynamic JSON in JAX-RS

I have to map a JSON to Java PoJos using JAX-RS (Resteasy as implementation). 我必须使用JAX-RS(Resteasy作为实现)将JSON映射到Java PoJos。 The problem is, that the JSON is dynamic. 问题是,JSON是动态的。 Look at this example: 看看这个例子:

{
  "typeCode": "SAMPLE",
  "data": [
    {
      "id": "COMMENTS",
      "answerValue": {
        "type": "YesNoAnswer",
        "value": true
      }
    },
    {
      "id": "CHOICE",
      "answerValue": {
        "type": "SelectListAnswer",
        "values": ["choice1", "choice2"]
      }
    }
  ]
}

The dynamic elements are in the data array. 动态元素位于数据数组中。 In principal every entry has an ID and an answerValue. 原则上,每个条目都有一个ID和一个answerValue。 But the answerValue is dynamic. 但answerValue是动态的。 Depending on his type he can have a single value (boolean, string, number an object) or an array of values. 根据他的类型,他可以有一个值(布尔值,字符串,数字对象)或值数组。

How can I map this to my Java model? 如何将其映射到我的Java模型?

I suggest you handle it by getting the answerValue node as a JsonNode type, and processing it manually to the Java type you need. 我建议您通过将answerValue节点作为JsonNode类型并将其手动处理为您需要的Java类型来处理它。

Something along the lines of the following: 以下内容:

class Data {
    public String typeCode;
    public List<Answer> data;
}

class Answer {
    public String id;

    public void setAnswerValue(JsonNode node) {
        String type = node.path("type").asText();

        switch (type) {
            case "YesNoAnswer" :
                boolean value = node.path("value").asBoolean();
                // TODO Handle
                break;
            case "SelectListAnswer" :
                JsonNode values = node.path("values");
                for (JsonNode v : values) {
                    String s = v.textValue();
                    // TODO Handle
                }

                break;
          }
    }
}

Your input can then be read with an ObjectMapper : 然后可以使用ObjectMapper读取您的输入:

ObjectMapper om = new ObjectMapper();
Data data = om.readValue(input, Data.class);

Thanks to the @Henrik of his solution. 感谢@Henrik的解决方案。 While implementing his proposal, I found a different solution, which suites better for me. 在实施他的提案时,我找到了一个不同的解决方案,这对我来说更适合。 I just use JsonSubTypes Annotation to handle inheritance. 我只是使用JsonSubTypes Annotation来处理继承。 This is my example: 这是我的例子:

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

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.PROPERTY,
        property = "type")
@JsonSubTypes({
        @JsonSubTypes.Type(value = YesNoAnswer.class, name = "YesNoAnswer"),
        @JsonSubTypes.Type(value = SelectListAnswer.class, name="SelectListAnswer"),
        @JsonSubTypes.Type(value = SelectAddressAnswer.class, name="SelectAddressAnswer")})
abstract class RequestFormAnswer {

    private String type;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM