【学习Redis】使用RedisTemplate设置值后出现\xac\xed\x00\x05t\x00\x02的问题解决方法

最近在学习Redis,学到加减运算部分,遇到一个问题,使用RedisTemplate设置值后出现\xac\xed\x00\x05t\x00\x02的问题
在这里插入图片描述
控制台报错:

【学习Redis】使用RedisTemplate设置值后出现\xac\xed\x00\x05t\x00\x02的问题解决方法_第1张图片


org.springframework.dao.InvalidDataAccessApiUsageException: ERR value is not an integer or out of range; nested exception is redis.clients.jedis.exceptions.JedisDataException: ERR value is not an integer or out of range

	at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:64)
	at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:41)
	at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:44)
	at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:42)
	at org.springframework.data.redis.connection.jedis.JedisConnection.convertJedisAccessException(JedisConnection.java:142)
	at org.springframework.data.redis.connection.jedis.JedisStringCommands.convertJedisAccessException(JedisStringCommands.java:815)
	at org.springframework.data.redis.connection.jedis.JedisStringCommands.decr(JedisStringCommands.java:475)
	at org.springframework.data.redis.connection.DefaultedRedisConnection.decr(DefaultedRedisConnection.java:344)
	at com.study.test.TestRedis.testCal(TestRedis.java:130)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR value is not an integer or out of range
	at redis.clients.jedis.Protocol.processError(Protocol.java:127)
	at redis.clients.jedis.Protocol.process(Protocol.java:161)
	at redis.clients.jedis.Protocol.read(Protocol.java:215)
	at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)
	at redis.clients.jedis.Connection.getIntegerReply(Connection.java:265)
	at redis.clients.jedis.BinaryJedis.decr(BinaryJedis.java:708)
	at org.springframework.data.redis.connection.jedis.JedisStringCommands.decr(JedisStringCommands.java:473)
	... 24 more

分析:
Spring中操作redis,键是使用StringRedisSerializer序列化器,值使用JdkSerializationRedisSerializer序列化器,而redis运算中只支持String类型的数据,所以只要将值序列化器换成StringRedisSerializer序列化器即可解决

解决方法:
在applicationContext.xml的配置文件中


  <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
    <property name="connectionFactory" ref="jedisConnectionFactory"/>
    <property name="keySerializer" ref="stringRedisSerializer"/>



      <property name="valueSerializer" ref="stringRedisSerializer"/>
  bean>

测试:

	/**
     * 打印当前key的值
     * @param redisTemplate redisTemplate
     * @param key key值
     */
    private void printCurrValue(RedisTemplate redisTemplate, String key) {
     
        String value = (String) redisTemplate.opsForValue().get(key);
        System.err.println(value + "\n");
    }

    /**
     * 测试redis运算
     */
    @Test
    public void testCal(){
     
		//加载配置文件
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("applicationContext.xml");

        //获取bean
        RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);

        //设置值
        redisTemplate.opsForValue().set("i", "20");
        System.out.println("set(\"i\", \"20\")");
        printCurrValue(redisTemplate, "i");

        //值减1
        redisTemplate.getConnectionFactory().getConnection().decr(
                redisTemplate.getKeySerializer().serialize("i")
        );
        System.out.println("decr -- serialize");
        printCurrValue(redisTemplate, "i");

        //值减6
        redisTemplate.getConnectionFactory().getConnection().decrBy(
                redisTemplate.getKeySerializer().serialize("i"), 6
        );
        System.out.println("decrBy -- serialize 6");
        printCurrValue(redisTemplate,"i");

        //值加浮点数2.3
        redisTemplate.opsForValue().increment("i", 2.3);
        System.out.println("increment(\"i\", 2.3)");
        printCurrValue(redisTemplate,"i");

    }

运行结果:
【学习Redis】使用RedisTemplate设置值后出现\xac\xed\x00\x05t\x00\x02的问题解决方法_第2张图片
现在redis客户端中查询数据也没有特殊字符了。
在这里插入图片描述

你可能感兴趣的:(javaweb)