寻时……
2020-11-25 09:23
采纳率: 50%
浏览 295
已采纳

fastJSON反序列化时list为null

小白第一次接触集群开发,使用谷粒商城作为练手项目,做秒杀服务时,使用fastJSON中 

JSON.parseObject(jsonString,tTypeReference);

方法,打印出来的list是null,百度无果,故求大神。

下面贴出代码

实体类

@AllArgsConstructor
@NoArgsConstructor
@Data
public class SeckillSesssionsWithSkus implements Serializable {

    private Long id;
    /**
     * 场次名称
     */
    private String name;
    /**
     * 每日开始时间
     */
    private Date startTime;
    /**
     * 每日结束时间
     */
    private Date endTime;

    private Integer status;

    private Date createTime;

    private List<SeckillSkuVo> relationSkus;





}

工具类

public <T> T  getData(TypeReference<T> tTypeReference){
		Object data = get("data");
		System.err.println("data---->>>"+data.toString());
		String jsonString = JSON.toJSONString(data);
		System.err.println("jsonString----->>>"+jsonString);
		T t = JSON.parseObject(jsonString,tTypeReference);
		System.err.println("t----->>>>"+t.toString());
		return t;
	}

工具类打印出来的数据

data---->>>[{id=3, name=test, startTime=2020-11-24T16:00:00.000+00:00, endTime=2020-11-26T16:00:00.000+00:00, status=1, createTime=2020-11-24T02:48:16.000+00:00, relationsku=[{id=3, promotionId=null, promotionSessionId=3, skuId=1, seckillPrice=1, seckillCount=4, seckillLimit=2, seckillSort=99}, {id=4, promotionId=null, promotionSessionId=3, skuId=1, seckillPrice=1, seckillCount=1, seckillLimit=1, seckillSort=99}, {id=5, promotionId=null, promotionSessionId=3, skuId=5, seckillPrice=0, seckillCount=1, seckillLimit=1, seckillSort=55}]}]
jsonString----->>>[{"id":3,"name":"test","startTime":"2020-11-24T16:00:00.000+00:00","endTime":"2020-11-26T16:00:00.000+00:00","status":1,"createTime":"2020-11-24T02:48:16.000+00:00","relationsku":[{"id":3,"promotionSessionId":3,"skuId":1,"seckillPrice":1,"seckillCount":4,"seckillLimit":2,"seckillSort":99},{"id":4,"promotionSessionId":3,"skuId":1,"seckillPrice":1,"seckillCount":1,"seckillLimit":1,"seckillSort":99},{"id":5,"promotionSessionId":3,"skuId":5,"seckillPrice":0,"seckillCount":1,"seckillLimit":1,"seckillSort":55}]}]
t----->>>>[SeckillSesssionsWithSkus(id=3, name=test, startTime=Wed Nov 25 00:00:00 CST 2020, endTime=Fri Nov 27 00:00:00 CST 2020, status=1, createTime=Tue Nov 24 10:48:16 CST 2020, relationSkus=null)]
sessionData---->>>>[SeckillSesssionsWithSkus(id=3, name=test, startTime=Wed Nov 25 00:00:00 CST 2020, endTime=Fri Nov 27 00:00:00 CST 2020, status=1, createTime=Tue Nov 24 10:48:16 CST 2020, relationSkus=null)]

 

业务层  调用

sesssion.getRelationSkus()

为null

  private void  saveSessionInfos(List<SeckillSesssionsWithSkus> sesssions){
        sesssions.stream().forEach(sesssion->{
            Long startTime = sesssion.getStartTime().getTime();
            Long endTime = sesssion.getEndTime().getTime();

            String key =SESSIONS_CACHE_PREFIX + startTime+"_"+endTime;
            Boolean hasKey = redisTemplate.hasKey(key);
            if (!hasKey){

                List<String> collect = sesssion.getRelationSkus()
                        .stream()
                        .map(
                                item -> item.getPromotionSessionId()+"_"+item.getSkuId().toString()
                        )
                        .collect(Collectors.toList());
                //缓存活动信息
                redisTemplate.opsForList().leftPushAll(key,collect);
            }


        });

    }

 

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • qq_25376897 2020-11-25 11:37
    已采纳

    你的data里面用的是relationsku,SeckillSesssionsWithSkus里面的是relationSkus,一个小写s,一个大写S

    点赞 评论
  • qq_25376897 2020-11-25 11:37

    字段名称都对不上

    点赞 评论

相关推荐 更多相似问题