Deserialization through JCache failing with ClassCastException
See original GitHub issueI am using Redisson as Redis client to store a key-value pair <String, LocalDateTime>
. It’s configured to be used through the JCache API (JSR-107).
It’s storing values like 2018-01-23T11:59:34.997834
but in the cache#get
invocation is trying to return a String giving a ClassCastException . It compiles perfectly, cache#get
returning LocalDateTime
, but if fails in execution.
Am I missing something here or is there some problem using it with the JCache API?
@Test
public void getCacheInline() {
// Configuration
Config redissonCfg = new Config();
redissonCfg
.setCodec(new JsonJacksonCodec(buildObjectMapper()))
.useSingleServer()
.setAddress("redis://redis:6379");
MutableConfiguration<String, LocalDateTime> jcacheConfig = new MutableConfiguration<String, LocalDateTime>()
.setTypes(String.class, LocalDateTime.class)
.setExpiryPolicyFactory((Factory<ExpiryPolicy>) () -> new CreatedExpiryPolicy(new Duration(SECONDS, 100)));
Configuration<String, LocalDateTime> configuration = RedissonConfiguration.fromConfig(redissonCfg, jcacheConfig);
// Cache creation
Cache<String, LocalDateTime> cache = cacheManager.createCache(CACHE_NAME, configuration);
// Put
LocalDateTime expectedDateTime = LocalDateTime.now();
cache.put("testKey", expectedDateTime);
// Get
// In this line: java.lang.ClassCastException: java.base/java.lang.String cannot be cast to java.base/java.time.LocalDateTime
LocalDateTime actualDateTime = cache.get("testKey");
assertThat(actualDateTime, is(equalTo(expectedDateTime)));
}
private ObjectMapper buildObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.configure(READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
objectMapper.setSerializationInclusion(NON_NULL);
return objectMapper;
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Spring-data-redis @Cacheable java.lang.ClassCastException
I stored result into redis and then it throws another exception as below when call the API the second time from cache. java.lang....
Read more >ava.lang.ClassCastException: java.lang.Clas... - JBoss.org
This starts as a database error in an EJB (a new Database Table can't be found -- I messed up in my Database...
Read more >Implementing Portable Serialization
This way, Hazelcast navigates in the byte[] and deserializes only the required field without actually deserializing the whole object.
Read more >javax.cache.CacheException.<init> java code examples | Tabnine
MBeanRegistrationException | NotCompliantMBeanException e) { throw new ... catch (IOException e) { throw new CacheException("Failed to deserialize", ...
Read more >Ehcache API Developer Guide - Software AG Documentation
A CacheManager can be created using either Java or the JSR 107 JCACHE ... differ from the ones expected, the CacheManager throws a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m seeing that wrapping in another object is working.
It serialised a json string with
@class
property, and then at deserialising time it has all the types there.Anyway, it doesn’t work with plain LocalDateTime objects, or any object from Java 8 Time API I would say.
@mrniko, Redisson team, I leave to you progressing this issue any further. As I cannot wait much longer, I think this workaround will do it for me if not I’ll go with something different.
Thanks
I am seeing this issue with RMapCache now. But, in my case, there could be many other object types that could be the values of this map (I have one RMapCache for whole application, this was recommended in one of the redisson documentation). I can not use the JsonJacksonMapCodec.
If I wrap the LocalDate in another object like an ArrayList, it is being serialized with @class and deserializing properly. But, just LocalDate is throwing exception and there is @class or any information about LocalDate for it to use while deserializing it.
Am I missing something? Any help would be greatly appreciated.