Can not deserialize instance of java.lang.Boolean out of START_OBJECT token

使用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;
}

成功。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章