jedis.hmset()方法存<key, Map>,导致redis.clients.jedis.exceptions.JedisDataException: value sent to redis cannot be null异常问题

需要存的Map对象结构类似于:

Map result = new HashMap();

result.put("a", "a");
result.put("b", studentInfo);
result.put("c", null);

jedis.hmset("orgKey", result);

异常信息:

[error] application - jedis error
redis.clients.jedis.exceptions.JedisDataException: value sent to redis cannot be null
	at redis.clients.util.SafeEncoder.encode(SafeEncoder.java:28)
	at redis.clients.jedis.Client.hmset(Client.java:184)
	at redis.clients.jedis.Jedis.hmset(Jedis.java:704)
	...

打断点result也不为null,怎么报错不能为null呢,进入查看了源码方法才发现:

public void hmset(final String key, final Map<String, String> hash) {
    final Map<byte[], byte[]> bhash = new HashMap<byte[], byte[]>(hash.size());
    for (final Entry<String, String> entry : hash.entrySet()) {
      bhash.put(SafeEncoder.encode(entry.getKey()), SafeEncoder.encode(entry.getValue()));
    }
    hmset(SafeEncoder.encode(key), bhash);
  }

map的<key,value>的value不能为null

解决办法(在此仅贴上我自己的方法,有更好的可以评论哟),换成mset()方法保存结果,把Map转为Json字符串的形式保存:

jedis.mset(orgKey, JSONObject.toJSON(result).toString());
posted @ 2021-07-22 11:16  近朱朱者赤  阅读(1855)  评论(0编辑  收藏  举报