java - Spring Redis Null Pointer Exception on redisTemplate -
hi working java-redis integration jedis , came problem when trying setup project. new spring there might issues initial setup. code trying work with:
redisconfig.java
@configuration public class redisconfig { @bean public redisconnectionfactory jedisconnectionfactory() { jedispoolconfig poolconfig = new jedispoolconfig(); poolconfig.setmaxtotal(1000); jedisconnectionfactory jedisconnectionfactory = new jedisconnectionfactory(poolconfig); jedisconnectionfactory.sethostname(properties.getinstance().getpropertyvaluereader().getawsredisendpoint()); jedisconnectionfactory.setport(6379); return jedisconnectionfactory; } @bean public <k, v> redistemplate<string, v> getredistemplate() { jdkserializationredisserializer jackson2jsonredisserializer = new jdkserializationredisserializer(); redistemplate<string, v> redistemplate = new redistemplate<string, v>(); redistemplate.setconnectionfactory(jedisconnectionfactory()); redistemplate.setkeyserializer(new stringredisserializer()); redistemplate.setvalueserializer(jackson2jsonredisserializer); redistemplate.setdefaultserializer(jackson2jsonredisserializer); redistemplate.sethashvalueserializer(jackson2jsonredisserializer); redistemplate.sethashkeyserializer(new stringredisserializer()); return redistemplate; } }
cacherepository.java
public interface cacherepository<t, v> { v get(object key); void add(object rediskey, object key, t value); void add(t key, v value); void expire(object key); }
cacherepositoryimpl.java
@component public class cacherepositoryimpl<t, v> implements cacherepository<t, v> { @autowired protected redistemplate<string, v> redistemplate; @override public v get(object key) { } @override public void expire(object key) { } @override public void add(object rediskey, object key, t value) { } @override public void add(t key, v value) { } }
vendorinformationcacherepository.java
public class vendorinformationcacherepository extends cacherepositoryimpl<string, hashmap<string, list<vehicles>>> { private static final string key = "vendors"; @override public void add(string key, hashmap<string, list<vehicles>> value){ system.out.println(redistemplate); // prints null redistemplate.opsforhash().put(key, key, value); }
}
basecontroller.java
vendorinformationcacherepository vicr = new vendorinformationcacherepository(); vicr.add("testkey", mynewlist);
in vendorinformationcacherepository.java redistemplate.opsforhash().put(key, key, value);
gives null pointer exception. stack trace of error:
java.lang.nullpointerexception @ com.moveinsync.cache.cacherepositoryimpl.add(cacherepositoryimpl.java:35) @ com.moveinsync.controllers.basecontroller.redistest(basecontroller.java:97) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:498) @ org.springframework.web.method.support.invocablehandlermethod.doinvoke(invocablehandlermethod.java:221) @ org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod.java:137) @ org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:111) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlermethod(requestmappinghandleradapter.java:806) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:729) @ org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:85) @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:959) @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:893) @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:970) @ org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:861) @ javax.servlet.http.httpservlet.service(httpservlet.java:621) @ org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:846) @ javax.servlet.http.httpservlet.service(httpservlet.java:728) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:305) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210) @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:51) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:243) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:222) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:123) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:502) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:171) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:100) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:953) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:118) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:408) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1041) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:603) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:310) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1149) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:624) @ java.lang.thread.run(thread.java:748)
any highly appreciated. in advance. cheers
based on line
system.out.println(redistemplate); // prints null
you have issues wiring redis template. don't think bean declaration generics job. instead, define bean redistemplate<string,object>
:
@bean public redistemplate<string, object> redistemplate() { ... }
start simple integration test confirm wire proper resttemplate
instance repository.
Comments
Post a Comment