After I cache the single item data on first run, the subsequent runs generate a cannot be cast to error.I would like to solve the issue of fetching the single item after I store it in the cache.
This is a gradle Spring application which stores data in MySQL database and uses redis as the cache manager.
SERVICE IMPLEMENTOR CLASS
@Autowired
private ClientRepository clientRepository;
//This doesn't work
@Override
@Cacheable(value = "clientCache", key = "{#clientId}")
public Client getClientById(long clientId) {
System.out.println("--- Inside getClientById() ---");
return clientRepository.findById(clientId).get();
}
//This works
@Override
@Cacheable(value = "allClientsCache")
public List<Client> getAllClients() {
System.out.println("--- Inside getAllClients() ---");
List<Client> list = new ArrayList<>();
clientRepository.findAll().forEach(e -> list.add(e));
return list;
}
CONTROLLER CLASS
@GetMapping("/client/{clientId}")
@ResponseBody
public ResponseEntity<Client> getClientById (@PathVariable Long clientId){
Client client = clientService.getClientById(clientId);
return new ResponseEntity<>(client, HttpStatus.OK);
}
@GetMapping("/clients")
public ResponseEntity<List<Client>> getAllClients() {
List<Client> list = clientService.getAllClients();
return new ResponseEntity<>(list, HttpStatus.OK);
}
REDIS CONFIG CLASS
@Autowired
private Environment env;
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration redisConf = new RedisStandaloneConfiguration();
return new LettuceConnectionFactory(redisConf);
}
@Bean
public RedisCacheManager cacheManager() {
RedisCacheManager rcm = RedisCacheManager.create(redisConnectionFactory());
rcm.setTransactionAware(true);
return rcm;
}
@Bean
public RedisTemplate<?, ?> redisTemplate() {
final RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setEnableTransactionSupport(true);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
I expect that on the browser i will see a single json client data
The error displayed on querying a single item is:
There was an unexpected error (type=Internal Server Error, status=500).
com.example.redis.Models.ClientModel.Client cannot be cast to com.example.redis.Models.ClientModel.Client
java.lang.ClassCastException: com.example.redis.Models.ClientModel.Client cannot be cast to com.example.redis.Models.ClientModel.Client
at com.example.redis.ServiceImplementers.Client.ClientServiceImpl$$EnhancerBySpringCGLIB$4620ドルf242.getClientById(<generated>)
at com.example.redis.Controllers.ClientController.ClientController.getClientById(ClientController.java:23)
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:190)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
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)
On terminal, redis has stored the item as
redis 127.0.0.1:6379> mget "clientCache::2"
1) "\xac\xed\x00\x05sr\x00\x1ecom.example.redis.Model.Client\x00\x00\x00\x00\x00\x00\x00\x01\x02\x00\nI\x00\x06activeJ\x00\bclientIdI\x00\x0bclient_codeI\x00\ncountry_idJ\x00\x06msisdnI\x00\x05phoneL\x00\aaddresst\x00\x12Ljava/lang/String;L\x00\x0bclient_nameq\x00~\x00\x01L\x00\x0bdescriptionq\x00~\x00\x01L\x00\x05emailq\x00~\x00\x01xp\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\a\x9b\x00\x00\x00\x03\x00\x00\x00hI\xa8\x00N)\xcb\xfcHt\x00\aKampalat\x00\x0cMTN Holdingst\x03wLorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.t\x00\[email protected]"
1 Answer 1
https://stackoverflow.com/a/52649099/11305437
This one sorted it out