今天做项目 需要使用springboot与redis集成 现在记录如下:
1,xml文件中插入redis的启动包,
<!-- redis启动类 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.编写yml文件中的redis配置
redis:
database: 0
host: 127.0.0.1
port: 6379
password:
timeout: 1000ms
jedis:
pool:
max-active: 200
max-idle: 10
min-idle: 0
max-wait: -1ms
3. 编写测试类:
package com.delta.whLab.user.test;
import static org.junit.Assert.*;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.delta.whLab.Utils.RedisUtil;
import io.swagger.Swagger2SpringBoot;
@SpringBootTest(classes = Swagger2SpringBoot.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class reditTest {
@Resource
private RedisTemplate<String, String> redisTemplate;
@Autowired
private RedisUtil redisUtil;
@Test
public void test() {
this.redisTemplate.opsForValue().set("studyTest", "java-hjtest");
//redisUtil.set("studyTest", "hj-test");
System.out.println(this.redisTemplate.opsForValue().get("studyTest"));
}
}
4. 这样做后 因为RedisTemplate 做序列化的时候是使用的char[] 而不是使用的String,导致key的值会带有\xAC\xED\x00\x05t\x00\x05 虽然不影响程序的读key中的值,但是在使用工具查看的时候或者使用redis的client读取key的值的时候会有问题,那么我们需要修改RedisTemplate的序列化类,如下:
package io.swagger.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
@Configuration
public class RedisConfiguration {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 使用Jackson2JsonRedisSerialize 替换默认序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// 设置value的序列化规则和 key的序列化规则
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
最后启动测试类,打印出了真实的结果,使用工具看时,也是正常的了。