将JSON转换为Java POJO时出现异常

yptwkmov  于 4个月前  发布在  Java
关注(0)|答案(1)|浏览(62)

我正在尝试从JSON转换到Java POJO。
我的JSON字符串看起来像这样:

{
  "data" : {
    "allIds" : [ {
      "model" : "relationships",
      "id" : "test"
    } ],
    "byId" : {
      "alerts" : { },
      "relationships" : {
        "test" : {
          "vertices" : [ {
            "model" : "services",
            "id" : "2"
          }, {
            "model" : "services",
            "id" : "11"
          }, {
            "model" : "services",
            "id" : "10"
          } ],
          "edges" : [ {
            "from" : "10",
            "to" : "11",
            "type" : "",
          
          }, {
            "from" : "10",
            "to" : "2",
            "type" : "",
          
          } ],
          "id" : "topology",
          "description" : "",
          "views" : [ ]
        }
      },
      "resources" : {
        "1" : {
          "id" : "1",
          "displayName" : "anl"
        },
        "6" : {
          "id" : "6",
          "displayName" : "sci"
        },
        "7" : {
          "id" : "7",
          "displayName" : "nam"
        }
      },
      "services" : {
        "11" : {
          "id" : "11",
          "type" : "services"
        },
        "2" : {
          "id" : "2",
          "type" : "services",
         
        },
        "10" : {
          "id" : "10",
          "type" : "services",
        }
      },
      "dataSourceInstances" : { }
    }
  },
  "meta" : {

  },
  "errors" : [ ],
  "warnings" : [ ]
}

字符串
POJO类

package com.santaba.server.servlet.rest.v4.pojos.traces;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import java.util.Map;

@JsonIgnoreProperties(ignoreUnknown = true)
public class TopologyResponse {

    @JsonProperty("data")
    private Data data;

    @JsonProperty("meta")
    private Meta meta;

    @JsonProperty("errors")
    private List<String> errors;

    @JsonProperty("warnings")
    private List<String> warnings;

    public Data getData() {
        return data;
    }

    public void setData(Data data) {
        this.data = data;
    }

    public Meta getMeta() {
        return meta;
    }

    public void setMeta(Meta meta) {
        this.meta = meta;
    }

    public List<String> getErrors() {
        return errors;
    }

    public void setErrors(List<String> errors) {
        this.errors = errors;
    }

    public List<String> getWarnings() {
        return warnings;
    }

    public void setWarnings(List<String> warnings) {
        this.warnings = warnings;
    }

    public static class Data {

        @JsonProperty("allIds")
        private List<AllId> allIds;

        @JsonProperty("byId")
        private ById byId;

        public List<AllId> getAllIds() {
            return allIds;
        }

        public void setAllIds(
            List<AllId> allIds) {
            this.allIds = allIds;
        }

        public ById getById() {
            return byId;
        }

        public void setById(ById byId) {
            this.byId = byId;
        }

        public static class AllId {

            @JsonProperty("model")
            private String model;

            @JsonProperty("id")
            private String id;

            public String getModel() {
                return model;
            }

            public void setModel(String model) {
                this.model = model;
            }

            public String getId() {
                return id;
            }

            public void setId(String id) {
                this.id = id;
            }
        }

        public static class ById {

            @JsonProperty("alerts")
            private Map<String, Object> alerts;

            @JsonProperty("relationships")
            private Relationships relationships;

            @JsonProperty("resources")
            private Map<String, Resources> resources;

            @JsonProperty("services")
            private Map<String, Services> services;

            @JsonProperty("dataSourceInstances")
            private Map<String, Object> dataSourceInstances;

            public Map<String, Object> getAlerts() {
                return alerts;
            }

            public void setAlerts(Map<String, Object> alerts) {
                this.alerts = alerts;
            }

            public Relationships getRelationships() {
                return relationships;
            }

            public void setRelationships(
                Relationships relationships) {
                this.relationships = relationships;
            }

            public Map<String, Resources> getResources() {
                return resources;
            }

            public void setResources(
                Map<String, Resources> resources) {
                this.resources = resources;
            }

            public Map<String, Services> getServices() {
                return services;
            }

            public void setServices(
                Map<String, Services> services) {
                this.services = services;
            }

            public Map<String, Object> getDataSourceInstances() {
                return dataSourceInstances;
            }

            public void setDataSourceInstances(Map<String, Object> dataSourceInstances) {
                this.dataSourceInstances = dataSourceInstances;
            }

            public static class Relationships {

                @JsonProperty("apm_topology")
                private ApmTopology apmTopology;

                public ApmTopology getApmTopology() {
                    return apmTopology;
                }

                public void setApmTopology(
                    ApmTopology apmTopology) {
                    this.apmTopology = apmTopology;
                }

                public static class ApmTopology {

                    @JsonProperty("vertices")
                    private List<Vertex> vertices;

                    @JsonProperty("edges")
                    private List<Edge> edges;

                    @JsonProperty("id")
                    private String id;

                    @JsonProperty("description")
                    private String description;

                    @JsonProperty("views")
                    private List<View> views;

                    public List<Vertex> getVertices() {
                        return vertices;
                    }

                    public void setVertices(
                        List<Vertex> vertices) {
                        this.vertices = vertices;
                    }

                    public List<Edge> getEdges() {
                        return edges;
                    }

                    public void setEdges(
                        List<Edge> edges) {
                        this.edges = edges;
                    }

                    public String getId() {
                        return id;
                    }

                    public void setId(String id) {
                        this.id = id;
                    }

                    public String getDescription() {
                        return description;
                    }

                    public void setDescription(String description) {
                        this.description = description;
                    }

                    public List<View> getViews() {
                        return views;
                    }

                    public void setViews(
                        List<View> views) {
                        this.views = views;
                    }

                    public static class Vertex {

                        @JsonProperty("model")
                        private String model;

                        @JsonProperty("id")
                        private String id;

                        public String getModel() {
                            return model;
                        }

                        public void setModel(String model) {
                            this.model = model;
                        }

                        public String getId() {
                            return id;
                        }

                        public void setId(String id) {
                            this.id = id;
                        }
                    }

                    public static class Edge {

                        @JsonProperty("from")
                        private String from;

                        @JsonProperty("to")
                        private String to;

                        @JsonProperty("type")
                        private String type;

                        @JsonProperty("attributes")
                        private Map<String, Object> attributes;

                        public String getFrom() {
                            return from;
                        }

                        public void setFrom(String from) {
                            this.from = from;
                        }

                        public String getTo() {
                            return to;
                        }

                        public void setTo(String to) {
                            this.to = to;
                        }

                        public String getType() {
                            return type;
                        }

                        public void setType(String type) {
                            this.type = type;
                        }

                        public Map<String, Object> getAttributes() {
                            return attributes;
                        }

                        public void setAttributes(Map<String, Object> attributes) {
                            this.attributes = attributes;
                        }
                    }

                    public static class View {

                        @JsonProperty("rid")
                        private String rid;

                        @JsonProperty("resource")
                        private String resource;

                        @JsonProperty("edgeTypes")
                        private List<String> edgeTypes;

                        public String getRid() {
                            return rid;
                        }

                        public void setRid(String rid) {
                            this.rid = rid;
                        }

                        public String getResource() {
                            return resource;
                        }

                        public void setResource(String resource) {
                            this.resource = resource;
                        }

                        public List<String> getEdgeTypes() {
                            return edgeTypes;
                        }

                        public void setEdgeTypes(List<String> edgeTypes) {
                            this.edgeTypes = edgeTypes;
                        }
                    }
                }
            }

            public static class Resources {

                @JsonProperty("resources")
                private Map<String, Resource> resources;

                public Map<String, Resource> getResources() {
                    return resources;
                }
                
                public void setResources(
                    Map<String, Resource> resources) {
                    this.resources = resources;
                }

                public static class Resource {

                    @JsonProperty("id")
                    private String id;

                    @JsonProperty("displayName")
                    private String displayName;

                    public String getId() {
                        return id;
                    }

                    public void setId(String id) {
                        this.id = id;
                    }

                    public String getDisplayName() {
                        return displayName;
                    }

                    public void setDisplayName(String displayName) {
                        this.displayName = displayName;
                    }
                }
            }

            public static class Services {

                @JsonProperty("services")
                private Map<String, Service> services;

                public Map<String, Service> getServices() {
                    return services;
                }

                public void setServices(
                    Map<String, Service> services) {
                    this.services = services;
                }

                public static class Service {

                    @JsonProperty("id")
                    private String id;

                    @JsonProperty("type")
                    private String type;

                    @JsonProperty("displayName")
                    private String displayName;

                    @JsonProperty("namespace")
                    private String namespace;

                    @JsonProperty("rid")
                    private String rid;

                    @JsonProperty("ert")
                    private String ert;

                    @JsonProperty("status")
                    private String status;

                    @JsonProperty("name")
                    private String name;

                    @JsonProperty("alertStatusPriority")
                    private String alertStatusPriority;

                    @JsonProperty("managedEdgeTypes")
                    private List<String> managedEdgeTypes;

                    @JsonProperty("alerts")
                    private List<Object> alerts;

                    @JsonProperty("properties")
                    private List<Property> properties;

                    @JsonProperty("resources")
                    private List<Resources> resources;

                    @JsonProperty("accessible")
                    private boolean accessible;

                    @JsonProperty("isAutoDetected")
                    private boolean isAutoDetected;

                    @JsonProperty("filterName")
                    private String filterName;

                    @JsonProperty("alertActivatedAtMS")
                    private long alertActivatedAtMS;

                    @JsonProperty("clickable")
                    private boolean clickable;

                    @JsonProperty("autoDetected")
                    private boolean autoDetected;

                    public String getId() {
                        return id;
                    }

                    public void setId(String id) {
                        this.id = id;
                    }

                    public String getType() {
                        return type;
                    }

                    public void setType(String type) {
                        this.type = type;
                    }

                    public String getDisplayName() {
                        return displayName;
                    }

                    public void setDisplayName(String displayName) {
                        this.displayName = displayName;
                    }

                    public String getNamespace() {
                        return namespace;
                    }

                    public void setNamespace(String namespace) {
                        this.namespace = namespace;
                    }

                    public String getRid() {
                        return rid;
                    }

                    public void setRid(String rid) {
                        this.rid = rid;
                    }

                    public String getErt() {
                        return ert;
                    }

                    public void setErt(String ert) {
                        this.ert = ert;
                    }

                    public String getStatus() {
                        return status;
                    }

                    public void setStatus(String status) {
                        this.status = status;
                    }

                    public String getName() {
                        return name;
                    }

                    public void setName(String name) {
                        this.name = name;
                    }

                    public String getAlertStatusPriority() {
                        return alertStatusPriority;
                    }

                    public void setAlertStatusPriority(String alertStatusPriority) {
                        this.alertStatusPriority = alertStatusPriority;
                    }

                    public List<String> getManagedEdgeTypes() {
                        return managedEdgeTypes;
                    }

                    public void setManagedEdgeTypes(List<String> managedEdgeTypes) {
                        this.managedEdgeTypes = managedEdgeTypes;
                    }

                    public List<Object> getAlerts() {
                        return alerts;
                    }

                    public void setAlerts(List<Object> alerts) {
                        this.alerts = alerts;
                    }

                    public List<Property> getProperties() {
                        return properties;
                    }

                    public void setProperties(
                        List<Property> properties) {
                        this.properties = properties;
                    }

                    public List<Resources> getResources() {
                        return resources;
                    }

                    public void setResources(List<Resources> resources) {
                        this.resources = resources;
                    }

                    public boolean isAccessible() {
                        return accessible;
                    }

                    public void setAccessible(boolean accessible) {
                        this.accessible = accessible;
                    }

                    public boolean isAutoDetected() {
                        return isAutoDetected;
                    }

                    public void setAutoDetected(boolean autoDetected) {
                        isAutoDetected = autoDetected;
                    }

                    public String getFilterName() {
                        return filterName;
                    }

                    public void setFilterName(String filterName) {
                        this.filterName = filterName;
                    }

                    public long getAlertActivatedAtMS() {
                        return alertActivatedAtMS;
                    }

                    public void setAlertActivatedAtMS(long alertActivatedAtMS) {
                        this.alertActivatedAtMS = alertActivatedAtMS;
                    }

                    public boolean isClickable() {
                        return clickable;
                    }

                    public void setClickable(boolean clickable) {
                        this.clickable = clickable;
                    }

                    public static class Property {

                        @JsonProperty("key")
                        private String key;

                        @JsonProperty("value")
                        private Object value;

                        public String getKey() {
                            return key;
                        }

                        public void setKey(String key) {
                            this.key = key;
                        }

                        public Object getValue() {
                            return value;
                        }

                        public void setValue(Object value) {
                            this.value = value;
                        }
                    }
                }
            }
        }
    }

    public static class Meta {

        @JsonProperty("accessible")
        private boolean accessible;

        @JsonProperty("resourceCount")
        private int resourceCount;

        @JsonProperty("instanceCount")
        private int instanceCount;

        @JsonProperty("serviceCount")
        private int serviceCount;

        @JsonProperty("edgeCount")
        private int edgeCount;

        @JsonProperty("startTimeAtMS")
        private long startTimeAtMS;

        @JsonProperty("endTimeAtMS")
        private long endTimeAtMS;

        public boolean isAccessible() {
            return accessible;
        }

        public void setAccessible(boolean accessible) {
            this.accessible = accessible;
        }

        public int getResourceCount() {
            return resourceCount;
        }

        public void setResourceCount(int resourceCount) {
            this.resourceCount = resourceCount;
        }

        public int getInstanceCount() {
            return instanceCount;
        }

        public void setInstanceCount(int instanceCount) {
            this.instanceCount = instanceCount;
        }

        public int getServiceCount() {
            return serviceCount;
        }

        public void setServiceCount(int serviceCount) {
            this.serviceCount = serviceCount;
        }

        public int getEdgeCount() {
            return edgeCount;
        }

        public void setEdgeCount(int edgeCount) {
            this.edgeCount = edgeCount;
        }

        public long getStartTimeAtMS() {
            return startTimeAtMS;
        }

        public void setStartTimeAtMS(long startTimeAtMS) {
            this.startTimeAtMS = startTimeAtMS;
        }

        public long getEndTimeAtMS() {
            return endTimeAtMS;
        }

        public void setEndTimeAtMS(long endTimeAtMS) {
            this.endTimeAtMS = endTimeAtMS;
        }
    }
}


我得到一个例外:
com.fasterxml.Jackson.databind.exc.MismatchedInputException:无法构造com.server.servlet.rest.pojos.Response$Data$ById$Resources$Resource的示例(尽管至少存在一个Creator):没有要从[Source:(String)"{.Response$Data$ById["resources"]->java.util.LinkedHashMap["1"]->com.server.servlet.rest.pojos.Response$Data$ById$Resources["id"])
我到底做错了什么?

zlwx9yxi

zlwx9yxi1#

private Map<String, Resource> resource字段的@JsonProperty注解应为:

@JsonProperty("resources")

字符串
而不是:

@JsonProperty("resource")


.因为你的JSON字符串有"resources"属性。另外,为了一致性,我建议将该字段重命名为resources

测试JUnit 5测试用例示例

class ResourcesTest {
    public static class Resources {
        @JsonProperty("resources")
        private Map<String, Resources.Resource> resources;

        public Map<String, Resources.Resource> getResource() {
            return resources;
        }

        public void setResources(
                Map<String, Resources.Resource> resources) {
            this.resources = resources;
        }

        public static class Resource {

            private String id;

            private String displayName;

            public String getId() {
                return id;
            }

            public void setId(String id) {
                this.id = id;
            }

            public String getDisplayName() {
                return displayName;
            }

            public void setDisplayName(String displayName) {
                this.displayName = displayName;
            }
        }
    }

    static final String sampleJson = """
        {
          "resources": {
            "1": {
              "id": "1",
              "displayName": "animal"
            },
            "6": {
              "id": "6",
              "displayName": "scientist"
            },
            "7": {
              "id": "7",
              "displayName": "namegen"
            }
          }
        }
        """;

    @Test
    void testSampleJsonDeserialization() throws JsonProcessingException {
        final var objectMapper = new ObjectMapper();

        final var response = objectMapper.readValue(sampleJson, Resources.class);
        assertEquals("animal", response.getResource().get("1").getDisplayName());
        assertEquals("scientist", response.getResource().get("6").getDisplayName());
        assertEquals("namegen", response.getResource().get("7").getDisplayName());
    }
}

相关问题