简体   繁体   English

反序列化Map时出错 <IgnoredCaseKey, Object> 来自spring restTemplate的回应

[英]Error while deserializing Map<IgnoredCaseKey, Object> response from spring restTemplate

while calling API request from a service I get the following error: 从服务调用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"])

the json I'm trying to get is : 我想要的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"
    }
  }
} ]

the function which requests the API call is : 请求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));
    }


the Pojo of the class is: 该班的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);
    }
}```

I tried writing my own JsonDeserialize and it didn't work. 我尝试编写自己的JsonDeserialize,但它没有用。 If I get an object in the getForEntity and deserialize it with ObjectMapper by myself, a process which runs in steps it will eventually work but I'm looking for a better solution 如果我在getForEntity中获取一个对象并使用ObjectMapper反序列化它,那么一个按步骤运行的进程最终会有效,但我正在寻找更好的解决方案

Cannot deserialize instance of java.lang.String out of START_OBJECT token; 无法从START_OBJECT标记中反序列化java.lang.String实例;

This exception means that you screwed up Jackson mapping. 这个例外意味着你搞砸了杰克逊的映射。

My personal advice to you to simplify debugging, is when dealing with complex JSON structures, instead of mapping the whole structure at once, try doing so step by step. 我个人建议您简化调试,是在处理复杂的JSON结构时,而不是一次映射整个结构,尝试一步一步地这样做。 Going from the first level, deeper and deeper down slowly unveiling JsonNodes. 从第一级开始,越来越深入地慢慢揭开JsonNodes的面纱。

The error most likely is here, because IgnoredCaseKey is not a string, but a custom object. 错误最有可能在这里,因为IgnoredCaseKey不是字符串,而是自定义对象。 Try to declare attributes or a key-value pair as JsonNode, and walk downwards to find what's causing the error. 试图声明attributesa键值对作为JsonNode,步行向下找出是什么导致了错误。

So your first step would be to replace this: 所以你的第一步就是替换这个:

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

with this: 有了这个:

@JsonProperty(ATTRIBUTES_PREFIX)
private JsonNode attributes;

If that is not causing any errors, then the problem is this Map<IgnoredCaseKey, Object> is the wrong type for the given JSON object. 如果这不会导致任何错误,那么问题是这个Map<IgnoredCaseKey, Object>是给定JSON对象的错误类型。 If it does cause errors, the problem is elsewhere. 如果确实会导致错误,则问题出在其他地方。

Since the structure of the JSON you provided is not accurate, I can't provide you with a specific answer, but hope this direction will help you troubleshoot more effectively. 由于您提供的JSON结构不准确,我无法为您提供具体的答案,但希望此方向可以帮助您更有效地进行故障排除。

暂无
暂无

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

相关问题 从Kafka Streams反序列化对象时出错 - Error While Deserializing object from Kafka Streams Spring RestTemplate +将XML结果映射到Domain对象 - Spring RestTemplate + map XML result to Domain object Spring RestTemplate在转换为响应对象之前检查响应状态不会导致适当的转换器错误 - Spring RestTemplate checking response status before casting to response object leads to no suitable converter error 在spring boot resttemplate中将响应对象转换为实体 - Convert a response object to entity in spring boot resttemplate 从 restApi 响应反序列化数据到 java 中的 class object 时出错? - Error on deserializing data from an restApi response to an class object in java? 没有从字符串值反序列化的字符串参数构造函数/工厂方法 - 从 restTemplate 反序列化 json object 时出现异常 - no String-argument constructor/factory method to deserialize from String value - Exception while deserializing json object from restTemplate 从邮递员 Spring Mvc 获取时未从 resttemplate.exchange 获取响应正文 - Not getting Response Body from resttemplate.exchange while getting from postman Spring Mvc RestTemplate 从响应实体解析为对象 - RestTemplate parsing from Response entity to an Object 从 restTemplate 响应中读取另一个 object 中的列表 - Reading a list in another object from a restTemplate response Spring RestTemplate 映射 JSON 响应 Map 抛出 MismatchedInputException - Spring RestTemplate Mapping JSON response to Map throws MismatchedInputException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM