jfinal Cache获取redis值空指针异常

6.png

Cache获取springboot项目里的,redis值会报错。怎么解决啊?

java.lang.RuntimeException: java.io.IOException: java.lang.NullPointerException

at com.jfinal.plugin.redis.serializer.FstSerializer.valueFromBytes(FstSerializer.java:78)

at com.jfinal.plugin.redis.Cache.valueFromBytes(Cache.java:1217)

at com.jfinal.plugin.redis.Cache.get(Cache.java:91)

at com.jfinalshop.service.TokenService.getUser(TokenService.java:43)

at com.jfinalshop.interceptor.TokenInterceptor.intercept(TokenInterceptor.java:33)

at com.jfinal.aop.Invocation.invoke(Invocation.java:67)

at com.jfinalshop.interceptor.ExceptionInterceptor.intercept(ExceptionInterceptor.java:17)

at com.jfinal.aop.Invocation.invoke(Invocation.java:67)

at net.hasor.plugins.jfinal.HasorInterceptor.intercept(HasorInterceptor.java:42)

at com.jfinal.aop.Invocation.invoke(Invocation.java:67)

at com.jfinal.core.ActionHandler.handle(ActionHandler.java:78)

at com.jfinalshop.handler.RoutesHandler.handle(RoutesHandler.java:85)

at com.jfinal.plugin.druid.DruidStatViewHandler.handle(DruidStatViewHandler.java:81)

at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:73)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)

at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)

at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)

at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)

at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)

at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)

at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)

at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)

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:202)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)

at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:346)

at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)

at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)

at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:887)

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1684)

at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Thread.java:748)

Caused by: java.io.IOException: java.lang.NullPointerException

at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:243)

at com.jfinal.plugin.redis.serializer.FstSerializer.valueFromBytes(FstSerializer.java:75)

... 41 more

Caused by: java.lang.NullPointerException

at org.nustaq.serialization.FSTObjectInput.readObjectWithHeader(FSTObjectInput.java:353)

at org.nustaq.serialization.FSTObjectInput.readObjectInternal(FSTObjectInput.java:327)

at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:307)

at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:241)

... 42 more


评论区

杜福忠

2022-02-09 17:38

FstSerializer 序列化get失败了,那得看set进去的时候用的啥,set 和 get 得用同一种方式了,

jfinal爱好者22

2022-02-10 08:50

@杜福忠 好的明白

jfinal爱好者22

2022-02-21 09:35

两种方案解决 1:先获取Jedis对象,然后再get 。cache.getJedis().get("name");
2:在config类里面,设置RedisPlugin 的序列化方式,
RedisPlugin redisPlugin = new RedisPlugin("xxx", "localhost");
redisPlugin.setSerializer(new ISerializer() {
@Override
public byte[] keyToBytes(String key) {
return SafeEncoder.encode(key);
}
@Override
public String keyFromBytes(byte[] bytes) {
return SafeEncoder.encode(bytes);
}
@Override
public byte[] fieldToBytes(Object field) {
return valueToBytes(field);
}
@Override
public Object fieldFromBytes(byte[] bytes) {
return valueFromBytes(bytes);
}
@Override
public byte[] valueToBytes(Object value) {
return SafeEncoder.encode(value.toString());
}
@Override
public Object valueFromBytes(byte[] bytes) {
if(bytes == null || bytes.length == 0)
return null;
return SafeEncoder.encode(bytes);
}
});