org.springframework.data.redis.serializer.SerializationException: Could not read JSON: N/A

3 篇文章 0 订阅

**

GenericJackson2JsonRedisSerializer序列化遇到的坑

**

**

问题现象

**
工程使用redis作为缓存,起初使用GenericJackson2JsonRedisSerializer作为序列化反序列化工具。一般正常的对象(set,get齐全),序列化以及解序列化都没有问题。但是一旦某个对象只有get方法,而没有set方法。就无法正常反序列化。

Redis序列化设置jackson

  // hash的value序列化方式采用jackson
  redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
  // value序列化方式采用jackson
  redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
  

测试对象

public class Demo {
    private String name;
    private int age;
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public int getAge() { return age; }
    public void setAge(int age) { this.age = age; }
    public String getGrade(){ return  "grade1"; }
}

留意最后一个方法 getGrade(){ return “grade1”; } 只有get

reids序列化内容

{"@class":"cn.pxwell.showen.Demo","name":"张三","age":3,"grade":"grade1"}

**

反序列化时Error提示

**

	org.springframework.data.redis.serializer.SerializationException: Could not read JSON:Unrecognized field "grade" (class cn.pxwell.showen.Demo), not marked as ignorable (2 known properties: "name", "age"])		
	at [Source: (byte[])"{"@class":"cn.pxwell.showen.Demo","name":"张三","age":3,"grade":"grade1"}"; line: 1, column: 68] (through reference chain: cn.pxwell.showen.Demo["grade"]); nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "grade" (class cn.pxwell.showen.Demo), not marked as ignorable (2 known properties: "name", "age"])
	at [Source: (byte[])"{"@class":"cn.pxwell.showen.Demo","name":"张三","age":3,"grade":"grade1"}"; line: 1, column: 68] (through reference chain: cn.pxwell.showen.Demo["grade"])

error提示: Could not read JSON: N/A

改用fastJson作为序列化工具

redis配置文件用:
<property name="hashValueSerializer">
<bean class="com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer"/>
</property>
SpringBean用:
// hash的value序列化方式采用fastJson
redisTemplate.setHashValueSerializer(new GenericFastJsonRedisSerializer());
// value序列化方式采用fastJson
redisTemplate.setValueSerializer(new GenericFastJsonRedisSerializer());

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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注解。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值