报错:
java.lang.NullPointerException
redis默认是jdk序列化 写入redis时不方便查看 因此会对redis的key进行序列化
package cn.devops.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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;
/**
* 配置redis序列化 默认为jdk序列化不便于查看 一般为json序列化方式存储
* */
@Configuration
public class RedisConfig {
@Bean(name = "redisTemplate")
public RedisTemplate getRedisTemplate(RedisConnectionFactory factory){
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(factory);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer); //key的序列化类型
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);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); //value的序列化类型
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
实体类 将对象存储进redis
package cn.devops.model;
import lombok.Data;
import java.io.Serializable;
@Data
public class RedisInfo implements Serializable {
private Integer id;
private String name;
private Data createTime;
}
Redis基础操作类
package cn.devops.utils;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
@Component
@Slf4j
public class RedisUtils {
@Resource
private RedisTemplate redisTemplate;
/**
* 读取缓存 redis中key对应的值
* @param key
* @return
* */
public String get(final String key){
return redisTemplate.opsForValue().get(key);
}
/**
* 写入String类型 到redis
* */
public boolean set(final String key, String value){
boolean result = false;
try{
redisTemplate.opsForValue().set(key, value);
log.info("存入redis成功,key:{},value:{}",key, value);
result = true;
}catch (Exception e){
log.info("存入redis失败,key:{},value:{}",key, value);
e.printStackTrace();
}
return result;
}
/**
* 写入对象到redis Json格式
* */
public boolean setJsonString(final String key, Object value){
if (StringUtils.isBlank(key)){
log.info("redis key值为空");
return false;
}
try{
redisTemplate.opsForValue().set(key, JSON.toJSONString(value));
log.info("存入redis成功,key:{},value:{}",key, value);
return true;
}catch (Exception e){
log.info("存入redis失败,key:{},value:{}",key, value);
e.printStackTrace();
}
return false;
}
/**
* 更新缓存
* */
public boolean getAndSet(final String key, String value){
boolean result = false;
try{
redisTemplate.opsForValue().getAndSet(key,value);
result = true;
}catch (Exception e){
e.printStackTrace();
}
return result;
}
/**
* 删除缓存
* */
public boolean delete(final String key){
boolean result = false;
try{
redisTemplate.delete(key);
result = true;
}catch (Exception e){
e.printStackTrace();
}
return result;
}
/**
* 一个指定的 key 设置过期时间
* @param key
* @param time
* */
public boolean expire(String key, long time){
return redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
/**
* 根据key 获取过期时间
* @param key
* */
public long getTime(String key){
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 根据key 获取过期时间
* @param key
* */
public boolean hasKey(String key){
return redisTemplate.hasKey(key);
}
/**
* 移除指定key 的过期时间
* @param key
* */
public boolean persist(String key){
return redisTemplate.boundValueOps(key).persist();
}
//-------------------------------------------------------Set 类型----------------------
/**
* 将数据放入set缓存
* @param key 键
* */
public void sSet(String key, String value){
redisTemplate.opsForSet().add(key, value);
}
}
测试程序
package cn.devops;
import cn.devops.model.RedisInfo;
import cn.devops.utils.RedisUtils;
import lombok.Data;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
@Data
public class RedisTest {
@Resource
private RedisUtils redisUtils;
@Test
public void contextLoads(){
RedisInfo redisInfo = new RedisInfo();
redisInfo.setId(1);
redisInfo.setName("morey");
redisInfo.setCreateTime(redisInfo.getCreateTime());
//写入redis
redisUtils.setJsonString("redisInfo",redisInfo);
//从redis中获取
System.out.println("获取数据"+redisUtils.get("redisInfo"));
}
}
错误原因: 测试程序中没有对注册进spring容器中 缺少@RunWith(SpringRunner.class) 注解 导致spring无法识别