java - 反序列化 Spring RestTemplate 的 Map<IgnoredCaseKey, Object> 响应时出错

标签 java json jackson deserialization resttemplate

从服务调用 API 请求时出现以下错误:

org.springframework.http.converter.HttpMessageNotReadableException: 
JSON parse error: Cannot deserialize instance of `java.lang.String` out of 
START_OBJECT token; nested exception is 
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize 
instance of `java.lang.String` out of START_OBJECT token
 at [Source: (ByteArrayInputStream); line: 19, column: 30] (through reference chain: java.lang.Object[][0]->com.cellwize.network.mo.load.model.ManagedObject["a"]->java.util.LinkedHashMap["siPeriodicity"])

我想要获取的 json 是:

[ {
  "guid" : "e_guid_lncell:311|480|6535681",
  "uid" : "c3528280-0d50-3ad8-a5be-d2dbdfcb77fd",
  "parentUid" : "0306573b-e431-37ad-ae59-5435947548cf",
  "parentMoClass" : null,
  "originalId" : "025530_1",
  "metaType" : "sector",
  "vendor" : "e",
  "technology" : "4g",
  "references" : null,
  "updateCommandType" : null,
  "scopeId" : "f5ed648f-4b42-4033-9243-db2150840995",
  "snapshotId" : 2054717834413232,
  "base_key" : "025530_1;MC",
  "class" : "vsDataEUtranCellFDD",
  "a" : {
    "prsConfigIndexMapped" : "1",
    "siPeriodicity" : {
      "siPeriodicitySI1" : "16",
      "siPeriodicitySI10" : "64",
      "siPeriodicitySI2" : "64",
      "siPeriodicitySI3" : "64",
      "siPeriodicitySI4" : "64",
      "siPeriodicitySI5" : "64",
      "siPeriodicitySI6" : "64",
      "siPeriodicitySI7" : "64",
      "siPeriodicitySI8" : "64",
      "siPeriodicitySI9" : "64"
    }
  }
} ]

请求API调用的函数是:

    public Map<String, ManagedObject> mapMosToUids(Collection<String> uids) throws IOException {
        if (uids == null || uids.isEmpty()) {
            return Collections.emptyMap();
        }

        final String url = naasUrl + "/getMos/" + String.join(",", uids);
        ResponseEntity<ManagedObject[]> response = restTemplate.getForEntity(url, ManagedObject[].class);


        ManagedObject[] mos = response.getBody();
        return Arrays.stream(mos).collect(Collectors.toMap(mo -> mo.getUid().toString(), mo -> mo));
    }


该类的 Pojo 是:


@JsonIgnoreProperties({"_id"})
public class ManagedObject{
    public static final String ATTRIBUTES_PREFIX = "a";
    public static final String GEN_ATTRIBUTES_PREFIX = "gen";
    public static final String PHY_ATTRIBUTES_PREFIX = "phy";
    public static final String BIT_ATTRIBUTES_PREFIX = "bit";

    public static final String META_PREFIX = "meta";
    public static final String CLASS_COLUMN = "class";
    public static final String BASEKEY_COLUMN = "base_key";
    public static final String NET_CTRL_KEY = "net_ctrl_uid";
    public static final String NETWORK_CONTROLLER = "netCtrl";
    public static final String META_TYPE_COLUMN = "meta_type";
    public static final String GUID_COLUMN = "guid";
    public static final String UID_COLUMN = "uid";
    public static final String PARENT_UID_COLUMN = "parent_uid";

    private static final StringDeduplicator STRING_DEDUPLICATOR = new StringDeduplicator();

    private String guid;
    private UUID uid;
    private UUID parentUid;
    private String parentMoClass;
    private String originalId;
    @JsonProperty(BASEKEY_COLUMN)
    private String originalName;
    @JsonProperty(CLASS_COLUMN)
    private String moClass;
    private String metaType;
    private String vendor;
    private String technology;
    @JsonProperty(ATTRIBUTES_PREFIX)
    private Map<IgnoredCaseKey, Object> attributes;
    private Map<String, String> references;
    @JsonProperty(META_PREFIX)
    private Map<String, String> meta;
    @JsonProperty(GEN_ATTRIBUTES_PREFIX)
    private Map<String, String> genAttributes;
    private String updateCommandType;

    @JsonProperty(PHY_ATTRIBUTES_PREFIX)
    private Map<String,String> phyAttributes;
    private UUID scopeId;
    private Long snapshotId;

    public ManagedObject() {
        this.attributes = new HashMap<>();
        this.meta = new HashMap<>();
        this.genAttributes = new HashMap<>();
        this.phyAttributes = new HashMap<>();
    }

    public ManagedObject(UUID uid, UUID parentUid, String moClass, Map<String, String> attributes) {
        this();

        this.uid = uid;
        this.parentUid = parentUid;
        this.moClass = moClass;

        copyAttributes(attributes, this.attributes);
    }
}```

我尝试编写自己的 JsonDeserialize 但没有成功。 如果我在 getForEntity 中获取一个对象并自己用 ObjectMapper 反序列化它,一个分步运行的过程最终会起作用,但我正在寻找更好的解决方案

最佳答案

Cannot deserialize instance of java.lang.String out of START_OBJECT token;

此异常意味着您搞砸了 Jackson 映射。

我个人对简化调试的建议是,在处理复杂的 JSON 结构时,不要一次映射整个结构,而是尝试逐步执行此操作。从第一层开始,越来越深地慢慢揭开 JsonNodes 的面纱。

错误最有可能就在这里,因为IgnoredCaseKey不是字符串,而是自定义对象。尝试声明attributesa键值对作为 JsonNode,然后向下查找导致错误的原因。

所以你的第一步是替换它:

@JsonProperty(ATTRIBUTES_PREFIX)
private Map<IgnoredCaseKey, Object> attributes;

这样:

@JsonProperty(ATTRIBUTES_PREFIX)
private JsonNode attributes;

如果这没有导致任何错误,那么问题就出在 Map<IgnoredCaseKey, Object> 上。给定 JSON 对象的类型错误。如果确实导致错误,则问题出在其他地方。

由于您提供的JSON结构不准确,我无法为您提供具体答案,但希望这个方向能够帮助您更有效地排除故障。

关于java - 反序列化 Spring RestTemplate 的 Map<IgnoredCaseKey, Object> 响应时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56567119/

相关文章:

java - 如何使用 libsvm 计算多类预测的概率?

java - 类有 Jackson json 注释吗?

java - JAX-RS JAXB Jackson 不使用@XmlRootElement 名称

java - userobjs 无法解析为变量

java - Java 包是否应该抛出其依赖模块的已检查异常

java - 动态包含在 JSP 模板中

json - 如何将 JSON 文件保存到 csv 中

java - 使用 Jetty Websockets 的通用编码器和解码器

javascript - 如何在读取文件时在 JSON.parse() 之前删除特殊字符

Java自定义注释不可见