redis 保存对象 io.lettuce.core.RedisCommandExecutionException: ERR value is not an integer or out of ran

在尝试将对象保存到Redis时遇到错误,原始方法是直接保存JSON格式数据,但更新为新方法后出现`io.lettuce.core.RedisCommandExecutionException: ERR value is not an integer or out of range`异常。解决此问题需要调整序列化设置,正确配置后,程序运行正常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以前redis保存json格式数据都是这样写

    public boolean set(String key, Object value) {
        boolean result = false;
        try {
            // 使用 JSONObject.toJSONString() 处理要存储的数据
            stringRedisTemplate.opsForValue().set(key, JSONObject.toJSONString(value));
            result = true;
        } catch (Exception e) {
            logger.error("写入reids缓存失败! 错误信息为:", e);
        }
        return  result;

现在改成

    /** 
     * 普通缓存放入 
     * @param key 键 
     * @param value 值 
     * @return true成功 false失败 
     */  
    public boolean set(String key,Object value) {  
         try {  
            redisTemplate.opsForValue().set(key, value);  
            return true;  
        } catch (Exception e) {  
            e.printStackTrace();  
            return false;  
        }  

    }  

使用时

		Demo demo = new Demo();
		demo.setAge(AgeEnum.THREE);
		demo.setCost(new BigDecimal(200.98));
		demo.setMajor("测试的喽"); 
		redis.set("testDemo2", demo);
		
		Demo demo2 = (Demo) redis.get("testDemo2");
		System.out.println("取回的对象:" + demo2.getMajor() + "," + demo2.getAge());

运行报错,

Caused by: io.lettuce.core.RedisCommandExecutionException: ERR value is not an integer or out of range
	at io.lettuce.core.internal.ExceptionFactory.createExecutionException(ExceptionFactory.java:137)
	at io.lettuce.core.internal.ExceptionFactory.createExecutionException(ExceptionFactory.java:110)
	at io.lettuce.core.protocol.AsyncCommand.completeResult(AsyncCommand.java:120)
	at io.lettuce.core.protocol.AsyncCommand.complete(AsyncCommand.java:111)
	at io.lettuce.core.protocol.CommandHandler.complete(CommandHandler.java:704)
	at io.lettuce.core.protocol.CommandHandler.decode(CommandHandler.java:639)
	at io.lettuce.core.protocol.CommandHandler.channelRead(CommandHandler.java:560)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
	at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
	at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
	at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.lang.Thread.run(Unknown Source)

 

需要设置序列化设置

@Configuration
public class LettuceRedisConfig {

	@Bean
	public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory connectionFactory) {
		//创建 redisTemplate 模版
		RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
		
		//设置 value 的转化格式和 key 的转化格式
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());		
		
		//关联 redisConnectionFactory
		redisTemplate.setConnectionFactory(connectionFactory);
		
		return redisTemplate;   
	}
}   

设置之后运行Ok

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值