【Redis】问题记录:redisTemplate调用Lua脚本获取返回值时报错(Could not read JSON:Unrecognized token .....)解决办法

文章目录


正文

        redisTemplate配置可以看我的这篇文章:
【Redis】RedisTemplate序列化配置、解决LocalDateTime数据类型序列化问题icon-default.png?t=N7T8https://blog.youkuaiyun.com/zxy2361380031/article/details/134121106


一、问题概述:

        在使用了GenericJackson2JsonRedisSerializer配置RedisTemplate的序列化之后,当调用Lua脚本并获取字符串类型的返回值时却报错:

        org.springframework.data.redis.serializer.SerializationException: Could not read JSON:Unrecognized token 'test': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false') at [Source: (byte[])"test"; line: 1, column: 5]  at org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer.deserialize(GenericJackson2JsonRedisSerializer.java:253)

        而当Lua脚本的返回值为其他类型,或为使用redis.call()命令查询出的字符串时就能正常运行

        Java代码和Lua脚本分别为:

@Autowired
private RedisTemplate redisTemplate;

@Test
void luaTest() {
    DefaultRedisScript<Object> script = new DefaultRedisScript<>();
    script.setLocation(new ClassPathResource("lua/test.lua"));
    script.setResultType(Object.class);
    
    Object execute = redisTemplate.execute(script, Collections.emptyList());
    System.out.println(execute);
}
return "test" --运行失败
return 1111 -- 运行成功
return redis.call('get','mykey') -- 运行成功

二、原因分析:

        根据报错信息SerializationException: Could not read JSON:Unrecognized token 'test'以及后面的at org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer.deserialize不难得出是由于RedisTemplate的序列化错误而导致的。

        由于上面Lua脚本返回的字符串"test"不满足JSON格式,因此GenericJackson2JsonRedisSerializer序列化器在反序列化时会抛出异常。而1111不是字符串,而接收的类型为Object.class,因此会被序列化器默认序列化为Long型数据,从而正常运行。


三、解决方案:

        知道了原因,那么解决办法也很明显,下面提供几种解决方法:

        方案一(掩耳盗铃法 -- 推荐)

        编写Lua脚本时不让它返回字符串不就好了吗,一般用到Redis Lua脚本的业务逻辑常常都是利用redis的原子操作对多个不同Key储存的Value进行修改,当执行条件不满足时,返回一个Long型数据,我们再自己验证并处理就好了,干嘛返回个字符串嘛qwq。总之,尽量不要在Lua脚本中返回字符串。当然由于特殊的业务需要,不得不返回字符串时,可采取下面的方式。

        方案二(修改序列化器 -- 不推荐)

        当我们需要调用lua脚本时使用redisTemplate.setValueSerializer

(new StringRedisSerializer());修该默认的值序列化器,执行完Lua脚本时再改回来。(因为这个设置会全局修改序列化器)代码如下:

@Autowired
private RedisTemplate redisTemplate;

@Test
void luaTest() {
    DefaultRedisScript<Object> script = new DefaultRedisScript<>();
    script.setLocation(new ClassPathResource("lua/test.lua"));
    script.setResultType(Object.class);
    
    redisTemplate.setValueSerializer(new StringRedisSerializer()); // 修改为string序列化器
    Object execute = redisTemplate.execute(script, Collections.emptyList());
    redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // 改回Json序列化器
    
    System.out.println(execute);
}

        方案三 (使用StringRedisTemplate -- 推荐) 

         我们的RedisTemplate配置了GenericJackson2JsonRedisSerializer Json序列化器报错了,为何不换一个呢,但像方案二那样非常的麻烦。那有没有默认序列化器就为string类型的类呢,答案是有的。SpringBoot的spring-boot-starter-data-redis为我们提供一个类StringRedisTemplate,正好能够满足我们的需求,使用方法如下:

@Autowired
private StringRedisTemplate stringRedisTemplate;

@Test
void luaTest() {
    DefaultRedisScript<Object> script = new DefaultRedisScript<>();
    script.setLocation(new ClassPathResource("lua/test.lua"));
    script.setResultType(Object.class);
    
    Object execute = stringRedisTemplate.execute(script, Collections.emptyList());
    
    System.out.println(execute);
}

 完美运行! 当然@Autowired也可以改为:

@Autowired
private RedisTemplate stringRedisTemplate;
org.springframework.data.redis.serializer.SerializationException是一个反序列化异常,表示在将JSON字符串转换为对象出现了问题。具体地说,"Unrecognized field"意味着JSON字符串中包含了无法识别的字段。 这个异常通常发生在使用Spring框架的Redis序列化器,它试图将JSON字符串反序列化为Java对象。在你提供的引用中,出现了两个例子,它们都是由于JSON字符串中的字段与Java对象的属性不匹配而引发了异常。 为了解决这个问题,你可以采取以下几个方法: 1. 使用@JsonIgnoreProperties(ignoreUnknown = true)注解 这个注解可以在类级别上使用,用于告诉Jackson JSON库在反序列化过程中忽略未知的JSON字段。在你的问题中,你可以在相关的Java类上加上这个注解以忽略无法识别的字段。 2. 使用@JsonIgnore注解 另一种解决方法是在相关的setter、getter或is方法上使用@JsonIgnore注解。这个注解可以用来忽略指定的属性,在反序列化不会将其考虑在内。 综上所述,你可以根据具体情况选择其中一种方法来解决org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Unrecognized field异常。通过使用@JsonIgnoreProperties(ignoreUnknown = true)注解或在相关方法上使用@JsonIgnore注解,你可以告诉序列化器忽略无法识别的字段,从而避免这个异常的发生。 : org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Unrecognized field : org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Unrecognized field "accountNonLocked" : 解决办法:在类加上注解@JsonIgnoreProperties(ignoreUnknown = true)忽略实体中没有对应的json的key值,或者在set、get或is开头的方法上加上@JsonIgnore注解。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狐笙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值