当前位置:   article > 正文

Jackson:String转object反序列化失败_cannot deserialize value of type `java.lang.string

cannot deserialize value of type `java.lang.string` from object value (token

场景

消费mq时String转Object

代码

for (MessageExt msg : msgs) {
                String msgBody = new String(msg.getBody(), StandardCharsets.UTF_8);
                BinlogEvent binlogEvent = JsonUtil.silentString2Object(msgBody, BinlogEvent.class);
                binlogEvent.setPort(Long.valueOf(port));
                tConsumer.consume(binlogEvent);
            }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

binlogEvent.setPort空指针,说明binlogEvent为空

debug报错信息

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.lang.String` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: (String)"{ "before": {}, "after": { "id": 1019, "name": "ababababab", "score": 99 }, "source": { "version": "zzcdc:", "connector": "mysql", "name": "zzcdc:task:5017", "ts_ms": 1686193967, "db": "cdc", "table": "t1", "server_id": 192215017, "gtid": "", "file": "", "pos": 0, "row": 0, "partition_key": "5017:cdc:t1:1019" }, "op": "u", "ts_ms": 1686193967808, "transaction": null, "timestamp": "2023-06-08T11:12:47.808178366+08:00", "ts_u_ms": 1686193967808178}"; line: 1, column: 13] (through reference chain: com.zhuanzhuan.datasync.helper.mq.event.BinlogEvent["before"])
  • 1
  • 2

原因分析

Cannot deserialize value of type `java.lang.String` from Object value (token `JsonToken.START_OBJECT`)
  • 1

报错信息:意思是Object反序列化为String失败

String:

{
    "before": {},
    "after": {
        "id": 1019,
        "name": "ababababab",
        "score": 99
    },
    "source": {
        "version": "zzcdc:",
        "connector": "mysql",
        "name": "zzcdc:task:5017",
        "ts_ms": 1686193967,
        "db": "cdc",
        "table": "t1",
        "server_id": 192215017,
        "gtid": "",
        "file": "",
        "pos": 0,
        "row": 0,
        "partition_key": "5017:cdc:t1:1019"
    },
    "op": "u",
    "ts_ms": 1686193967808,
    "transaction": null,
    "timestamp": "2023-06-08T11:12:47.808178366+08:00",
    "ts_u_ms": 1686193967808178
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

承接实体Object:

public class BinlogEvent {
    private String before;
    private String after;
    private String source;
    private String op;
    private Long tsMs;
    private String transaction;
    private String timestamp;
    private Long tsUMs;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

发现原因:JSON/Object数据不能用String承接

解决

方法1:使用Object承接

public class BinlogEvent {
    private Object before;
    private Object after;
    private Object source
    private String op;
    private Long tsMs;
    private Object transaction;
    private String timestamp;
    private Long tsUMs;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

方法2:新建JSON对应实体类承接

 public class BinlogEvent {
    private Object before;
    private Object after;
    private BinlogSourceEvent source
    private String op;
    private Long tsMs;
    private Object transaction;
    private String timestamp;
    private Long tsUMs;
}

public class BinlogSourceEvent {
    private String version;
    private String connector;
    private String name;
    private Long tsMs;
    private String db;
    private String table;
    private Long serverId;
    private String gtid;
    private String file;
    private Long pos;
    private Long row;
    private String partitionKey;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/610115
推荐阅读
相关标签
  

闽ICP备14008679号