zoukankan      html  css  js  c++  java
  • 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());
    
  • 相关阅读:
    rabiitmq 消息丢失处理
    后端返回文件流和json格式的方式进行文件下载导出
    elasticsearch 安装
    docker-compose 命令
    艾孜尔江的部分影视作品
    游戏作品:万里同风
    Unity设置计时器
    Unity项目中VS Code部分功能出现问题解决思路
    Unity接收FFmpeg的UDP推流
    SVN Basics
  • 原文地址:https://www.cnblogs.com/zys-blog/p/15043220.html
Copyright © 2011-2022 走看看