Jackson:String转object反序列化失败

场景

消费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);
            }

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"])

原因分析

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

报错信息:意思是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
}

承接实体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;
}

发现原因: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;
}

方法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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值