引入Jedis 和 Redis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>${redis.version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
配置文件增加Redis配置-yml
spring:
redis:
#服务器地址
host: 192.168.195.99
#服务器端口
port: 6379
Redis服务器连接密码(默认为空)
password: wumenglong@1
#redis数据库索引
database: 0
jedis:
pool:
#jedis最大连接数 使用负值表示没有限制
max-active: 50
#jedis 连接池最大阻塞等待时间 使用负值表示没有限制
max-wait: 3000
#jedis连接池中最大空闲连接数
max-idle: 20
#jedis连接池中最空闲连接数
min-idle: 0
#连接超时时间
timeout: 5000
配置文件增加Redis配置-properties
# redis连接地址
spring.redis.host=192.168.195.99
# redis连接地址端口
spring.redis.port=6379
# redis密码
# spring.redis.password=
# redis数据库索引
spring.redis.database=0
# redis最小空闲连接数
spring.redis.jedis.pool.min-idle=0
# redis最大空闲连接数
spring.redis.jedis.pool.max-idle=20
# redis最大阻塞等待时间 -1不限制 单位:毫秒
spring.redis.jedis.pool.max-wait=3000
# redis连接数 -1不限制
spring.redis.jedis.pool.max-active=50
# redis连接服务器超时时间 单位毫秒
spring.redis.timeout=5000
Redis配置
/**
* 初始化Redis连接池
*/
static {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setBlockWhenExhausted(BLOCK_WHEN_EXHAUSTED);
config.setEvictionPolicyClassName(EVICTION_POLICY_CLASSNAME);
config.setJmxEnabled(JMX_ENABLED);
config.setJmxNamePrefix(JMX_NAME_PREFIX);
config.setLifo(LIFO);
config.setMaxIdle(MAX_IDLE);
config.setMaxTotal(MAX_TOTAL);
config.setMaxWaitMillis(MAX_WAIT);
config.setMinEvictableIdleTimeMillis(MIN_EVICTABLE_IDLE_TIME_MILLIS);
config.setMinIdle(MIN_IDLE);
config.setNumTestsPerEvictionRun(NUM_TESTS_PER_EVICYION_RUN);
config.setSoftMinEvictableIdleTimeMillis(SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
config.setTestOnBorrow(TEST_ON_BORROW);
config.setTestWhileIdle(TEST_WHILEIDLE);
config.setTimeBetweenEvictionRunsMillis(TIME_BERWEEN_EVICTION_RUNS_MILLIS);
jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT);
// jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, PASSWORD);
} catch (Exception e) {
log.error("Redis连接池 初始化异常" + e);
e.printStackTrace();
}
}
/**
* 获取连接
*/
public static Jedis getJedis() {
return jedisPool.getResource();
}
/**
* 释放连接
*/
public static void closeJedis(Jedis jedis) {
jedis.close();
}
封装Redis工具类
// 标识为一个组件
@Component
public class RedisUtil {
private static Jedis jedisTemplate;
// 在服务启动的时候执行被这个注释修饰的方法
@PostConstruct
public static void init(){
jedisTemplate = RedisConfig.getJedis();
}
public static String getRedisValueByKey(String key){
String str = jedisTemplate.get(key);
return str;
}
}
未解决问题
还有一种方法是redisTemplate,但是这种方法目前在本地报错空指针,一直没找到原因,下面是代码,等未来有空再解决,有知道的大佬可以指点一下
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.wzy.common.redis.RedisConfig;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
/**
* @author : wuzhenyu
* @date : 2022/9/19 17:10
* @effect : 当前类的作用是 redis工具类
* redis工具类
* key值默认添加 safe: 前缀,以便于分组管理
* 若需操作原生key ,则调用 redis() 获取StringRedisTemplate直接操作
*/
@Component
public class RedisUtil {
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Resource
private StringRedisTemplate stringRedisTemplate;
/**
* 读取缓存
*
* @param key
* @return
*/
public Object get(final String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* 写入缓存
*/
public boolean set(final String key, String value) {
boolean result = false;
try {
redisTemplate.opsForValue().set(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 更新缓存
*/
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;
}
/**
* 获取值并转换为对应bean
* @param k k
* @return {@link T }
**/
public <T> T getBean(String k, Class<T> clazz) {
String v = this.opsForValue().get(this.reSetKey(k));
if (JSONUtil.isJsonObj(v)) {
return JSONUtil.toBean(v, clazz);
}
return null;
}
/**
* 获取值并转换为对应bean
* @param k k
* @return {@link List <T> }
**/
public <T> List<T> getBeanList(String k, Class<T> clazz) {
String v = this.opsForValue().get(this.reSetKey(k));
if (JSONUtil.isJsonArray(v)) {
List<T> list = JSONUtil.toList(v, clazz);
if (list != null) {
return list;
}
}
return new ArrayList<>(0);
}
/**
* 设置值
* @param k k
* @param v v
**/
public void set(String k, Object v) {
this.opsForValue().set(this.reSetKey(k), this.reSetValue(v));
}
/**
* 设置值
* @param k k
* @param v v
* @param timeout 有效时间 (单位 : 秒)
**/
public void set(String k, Object v, long timeout) {
this.opsForValue().set(this.reSetKey(k), this.reSetValue(v), timeout, TimeUnit.SECONDS);
}
/**
* 设置值
* @param k k
* @param v v
* @param timeout 有效时间
* @param timeUnit 时间单位
**/
public void set(String k, Object v, long timeout, TimeUnit timeUnit) {
this.opsForValue().set(this.reSetKey(k), this.reSetValue(v), timeout, timeUnit);
}
/**
* 根据表达式获取所有key
* @param pattern key表达式
* @return {@link Set <String> }
**/
public Set<String> keys(String pattern) {
return stringRedisTemplate.keys(this.reSetKey(pattern));
}
/**
* 设置有效期
* @param k k
* @param timeout 有效时间 (单位 : 秒)
* @return boolean
**/
public boolean expire(final String k, long timeout) {
Boolean flag = stringRedisTemplate.expire(this.reSetKey(k), timeout, TimeUnit.SECONDS);
return flag != null && flag;
}
/**
* 设置有效期
* @param k k
* @param timeout 有效时间
* @param timeUnit 时间单位
* @return boolean
**/
public boolean expire(final String k, long timeout, TimeUnit timeUnit) {
Boolean flag = stringRedisTemplate.expire(this.reSetKey(k), timeout, timeUnit);
return flag != null && flag;
}
/**
* 是否存在key
* @param k k
* @return boolean
**/
public boolean hasKey(String k) {
Boolean exists = stringRedisTemplate.hasKey(this.reSetKey(k));
return exists != null && exists;
}
/**
* 获取剩余有效期
* @param k k
* @return long (单位 : 秒)
**/
public long getExpire(String k) {
Long expire = stringRedisTemplate.getExpire(this.reSetKey(k), TimeUnit.SECONDS);
return expire == null ? 0 : expire;
}
/**
* 获取剩余有效期
* @param k k
* @param timeUnit 时间单位
* @return long 剩余有效期
**/
public long getExpire(String k, TimeUnit timeUnit) {
Long expire = stringRedisTemplate.getExpire(this.reSetKey(k), timeUnit);
return expire == null ? 0L : expire;
}
/**
* 删除指定key
* @param k k
* @return boolean 是否成功
**/
public boolean remove(String k) {
Boolean flag = stringRedisTemplate.delete(this.reSetKey(k));
return flag != null && flag;
}
/**
* 删除指定集合中的key
* @param keys key集合
* @return long (删除成功的 key 的数量)
**/
public long remove(Collection<String> keys) {
// 此处一般是通过 keys 方法获取的真实key值, 所以不用调用 reSetKey 方法
Long flag = stringRedisTemplate.delete(keys);
return flag == null ? 0L : flag;
}
/**
* 如果k不存在则设置v
* @param k k
* @param v v
* @return boolean 成功:true 失败:false
**/
public boolean setNx(String k, String v) {
return Convert.toBool(this.opsForValue().setIfAbsent(this.reSetKey(k), this.reSetValue(v)), false);
}
/**
* 如果k不存在则设置v
* @param k k
* @param v v
* @param timeout 超时时间 (秒)
* @return boolean 成功:true 失败:false
**/
public boolean setNx(String k, String v, long timeout) {
return Convert.toBool(this.opsForValue().setIfAbsent(this.reSetKey(k), this.reSetValue(v), timeout, TimeUnit.SECONDS), false);
}
/**
* 获取原生 stringRedisTemplate
* @return {@link StringRedisTemplate }
**/
public StringRedisTemplate redis() {
return stringRedisTemplate;
}
/**
* redis 添加统一前缀, 便于分组管理
* @param k k
* @return {@link String }
* @author zhangchao
* @date 2022-02-22 16:54
**/
private String reSetKey(String k) {
return StrUtil.concat(false, PREFIX, k);
}
/**
* 重新设置值
* 字符串类型直接存储
* 其他类型转换为 json字符串存储
* @param v v
* @return {@link String }
**/
private String reSetValue(Object v) {
if (v == null) {
return null;
}
if (v instanceof CharSequence) {
return StrUtil.str((CharSequence) v);
}
if (v instanceof Number) {
return NumberUtil.toStr((Number) v);
}
return JSONUtil.toJsonStr(v);
}
/**
* 获取 ValueOperations
* @return {@link ValueOperations }
**/
private ValueOperations<String, String> opsForValue() {
return stringRedisTemplate.opsForValue();
}
}
配置
package com.wzy.common.redis;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
import com.wzy.common.utils.RedisUtil;
import java.time.Duration;
import java.util.logging.Logger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
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.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* @author : wuzhenyu
* @date : 2022/9/19 10:46
* @effect : 当前类的作用是 redis 配置文件
*/
// @EnableCaching
// @Configuration
@Slf4j
public class RedisConfig extends CachingConfigurerSupport {
// @Value("${spring.redis.host}")
private static String ADDR = "192.168.195.99";
// @Value("${spring.redis.port}")
private static int PORT = 6379;
// private static String PASSWORD = "";
private static int MAX_TOTAL = 8;
private static int MIN_IDLE=0;
// @Value("${spring.redis.jedis.pool.max-idle}")
private static int MAX_IDLE = 1000;
private static int MAX_WAIT = -1;
// @Value("${spring.redis.timeout}")
private static int TIMEOUT = 10000;
private static boolean BLOCK_WHEN_EXHAUSTED = true;
private static String EVICTION_POLICY_CLASSNAME="org.apache.commons.pool2.impl.DefaultEvictionPolicy";
private static boolean JMX_ENABLED=true;
private static String JMX_NAME_PREFIX="pool";
private static boolean LIFO=true;
private static long MIN_EVICTABLE_IDLE_TIME_MILLIS=1800000L;
private static long SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS=1800000L;
private static int NUM_TESTS_PER_EVICYION_RUN=3;
private static boolean TEST_ON_BORROW = false;
private static boolean TEST_WHILEIDLE=false;
private static long TIME_BERWEEN_EVICTION_RUNS_MILLIS=-1;
private static JedisPool jedisPool = null;
/**
* 返回一个RedisTemplate对象
* @param factory
* @return
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
StringRedisSerializer redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(DefaultTyping.NON_FINAL);
template.setConnectionFactory(factory);
// key序列化方式
template.setKeySerializer(redisSerializer);
// value序列化
template.setValueSerializer(jackson2JsonRedisSerializer);
// value hashmap序列化
template.setHashValueSerializer(jackson2JsonRedisSerializer);
return template;
}
public CacheManager cacheManager(RedisConnectionFactory factory) {
StringRedisSerializer redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
// 解决查询缓存转换异常的问题
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
om.enableDefaultTyping(DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
// 配置序列化(解决乱码问题),过期时间600秒
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(600))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues();
RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
return cacheManager;
}
}
报错
java.lang.NullPointerException
at com.wzy.common.utils.RedisUtil.getAndSet(RedisUtil.java:83)
at com.wzy.loginregistration.service.impl.RegisterServiceImpl.ringUp(RegisterServiceImpl.java:165)
at com.wzy.loginregistration.service.impl.RegisterServiceImpl$$FastClassBySpringCGLIB$$7f118bce.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:783)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:698)
at com.wzy.loginregistration.service.impl.RegisterServiceImpl$$EnhancerBySpringCGLIB$$cfd30571.ringUp(<generated>)
at com.wzy.loginregistration.controller.RegisterController.logIn(RegisterController.java:59)
at com.wzy.loginregistration.controller.RegisterController$$FastClassBySpringCGLIB$$29198c20.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:783)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:64)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:58)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:698)
at com.wzy.loginregistration.controller.RegisterController$$EnhancerBySpringCGLIB$$330bf538.logIn(<generated>)
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.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:681)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:895)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1722)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at xmlorg.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:750)
报错代码
/**
* 更新缓存
*/
public boolean getAndSet(final String key, String value) {
boolean result = false;
try {
// redisTemplate NullPointerException
redisTemplate.opsForValue().getAndSet(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}