I have a Spring Boot app with Redis cache. I faced the an issue - when I put an object of class A to cache and then retrieve it back the object can't be casted to the class A.
Person person = new Person();getCache().put("person", person);Person p = getCache().get("person", Person.class); // throws java.lang.IllegalStateException: Cached value is not of required type [com.dummy.domain.Person]: Person(name=null)Object p = getCache().get("person").get();(Person) p // Cannot cast 'com.dummy.domain.Person' to 'com.dummy.domain.Person'
If I do getClass
on the object I receive the same fully qualified name of the of A-class, but I noticed that they have different ClassLoaders. getClass().getClassLoader()
gives:Initial object:
- name = null
- nameAndId = org.springframework.boot.devtools.restart.classloader.RestartClassLoader
- parent = AppClassLoader
The object from the cache:
- name = app
- name = app
- parent = PlatformClassLoader
What could be the reason changed ClassLoaders? I guess it is the source of the casting issue