SpringBoot整合Redis报错java.lang.NullPointerException
package com.bulldozer.config;
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;
/**
* redis配置类
* @author Abbott.liu
* @date 2021年5月7日
*/
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
package com.bulldozer.utils;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
/**
* Redis工具类
* @author Abbott.liu
* @date 2021年5月7日
*/
@Component
public final class RedisUtil {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 普通缓存放入
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public boolean set(String key, Object value) {
try {
ValueOperations<String, Object> vo = redisTemplate.opsForValue();
vo.set(key, value);
redisTemplate.expire(key, 1, TimeUnit.HOURS); // 这里指的是1小时后失效
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
RedisUtil redisUtil = new RedisUtil();
redisUtil.set("abc", "123");
}
}
又学习了一下
@Autowired
public StringRedisTemplate stringRedisTemplate;
@RequestMapping(value="/v1/refresh/time", method={ RequestMethod.POST })
public ResponseResult countNum() {
String code = "500";
String message = "FAIL";
String detail = "selectBulldozerInfo";
ResponseResult responseResult = new ResponseResult();
String userNum = stringRedisTemplate.opsForValue().get("timeRefreshWaring");
if(StringUtils.isNull(userNum)){
String dateStr = DateUtils.dateToString(new Date());
stringRedisTemplate.opsForValue().set("timeRefreshWaring", dateStr);
code = "200";
message = "SUCCESS";
detail = "refresh success";
JSONObject jsonResultObject = new JSONObject();
jsonResultObject.put("timeRefreshWaring", dateStr);
responseResult.setData(jsonResultObject);
}
responseResult.setCode(code);
responseResult.setMessage(message);
responseResult.setDetail(detail);
return responseResult;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
redisTemplate
怎么来的,spring boot注入的。手动
new RedisUtil();
,算spring boot注入?当然不算。。。。。。
这为啥空还不知道吗?