使用mq发消息时,消息体使用一个实体类接收,消息接收方反序列化时总是报错:
2019-10-10 13:29:34.609 [mro: e0ff3fbce8cfa095][http-nio-8002-exec-7] ERROR com.runlion.sat.tools.handler.ExceptionHandler - >>>>>>>>> 全局异常 JSON parse error: Can not deserialize instance of java.lang.Boolean out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.Boolean out of START_OBJECT token
at [Source: java.io.PushbackInputStream@27ba128; line: 1, column: 1] >>>>>>>>>>>>>>
1. 发送消息
//发送占用库存消息
OccupyTypeDTO occupyTypeDTO = new OccupyTypeDTO();
occupyTypeDTO.setId(sendOrderMainId);
occupyTypeDTO.setType(1);
occupyTypeDTO.setUserId(LoginUserUtil.getLoginUserId());
MqDTO mqDTO = new MqDTO();
mqDTO.setMqTypeEnum(OCCUPYSTRATEGY);
mqDTO.setData(JSONObject.toJSONString(occupyTypeDTO));
if (!mqProducerApi.producer(mqDTO)){
distributedLocker.unlock(String.valueOf(LockTypeEnum.SEND_ORDER_MAIN.getCode()) + sendOrderMainId);
throw new BaseException("发送占用库存消息失败");
}
2. 实体类OccupyTypeDTO
@Data
public class OccupyTypeDTO implements Serializable {
private Integer type;
private Long id;
private Long userId;
}
3. 消息接收方:
增加注解@RequestBody
public boolean occupyLocation(@RequestBody String data) {
OccupyTypeDTO occupyTypeDTO = JSON.parseObject(data, OccupyTypeDTO.class);
boolean boo = false;
//业务处理
return boo;
}
这样就会报错。
修改实体类OccupyTypeDTO,增加注解
@JsonSerialize(using = LongJsonSerializer.class)
@JsonDeserialize(using = LongJsonDeserializer.class)
@Data
public class OccupyTypeDTO implements Serializable {
private Integer type;
/**id**/
@JsonSerialize(using = LongJsonSerializer.class)
@JsonDeserialize(using = LongJsonDeserializer.class)
private Long id;
@JsonSerialize(using = LongJsonSerializer.class)
@JsonDeserialize(using = LongJsonDeserializer.class)
private Long userId;
}
成功。
在使用MQ发送包含实体类的消息时,接收方反序列化遇到问题,报'Can not deserialize instance of java.lang.Boolean out of START_OBJECT token'错误。通过在实体类的Long字段上添加@JsonSerialize和@JsonDeserialize注解,分别指定LongJsonSerializer和LongJsonDeserializer,成功解决了反序列化问题。
3246

被折叠的 条评论
为什么被折叠?



