欢迎光临Fuwu360     登录 | 免费注册 |
按CTRL+D收藏,下次访问更方便
当前位置: 首页 > 编程 > (no Creators, like default construct, exist): cannot deserialize from Object value报错的解决办法

浏览历史

(no Creators, like default construct, exist): cannot deserialize from Object value报错的解决办法

龙永超 / 2020-05-21

(no Creators, like default construct, exist): cannot deserialize from Object value报错的解决办法

一个Spring boot项目,使用了Redis的消息队列,在反序列化的时候报错:

org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Cannot construct instance of `com.springboot.twolevelcache.autoconfigure.support.CacheMessage` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

原因是实体类中没有写无参构造方法!

必须有无参的构造方法,否则Redis使用了GenericJackson2JsonRedisSerializer序列化器,在反序列化的时候就会报上面的这种异常 。

报错的地方,代码是:

redisTemplate.getValueSerializer().deserialize(message.getBody());

我的实体类是:

public class CacheMessage implements Serializable {

/** */
private static final long serialVersionUID = 5987219310442078193L;

private String cacheName;
private Set<Object> keys;

public CacheMessage(String cacheName, Set<Object> keys) {
super();
this.cacheName = cacheName;
this.keys = keys;
}

public String getCacheName() {
return cacheName;
}

public void setCacheName(String cacheName) {
this.cacheName = cacheName;
}

public Set<Object> getKeys() {
return keys;
}

public void setKeys(Set<Object> keys) {
this.keys = keys;
}
}

添加下面的 无参构造方法即可:

public CacheMessage(){}


      




微信或浏览器扫码阅读
用户评论 (共0条评论)

暂时还没有任何用户评论

总计 0 个记录,共 1 页。
亲,赐个留言吧!
captcha