spring-boot - NotSerializableException with redisson-spring-boot-starter
问题描述
I'm using redisson-spring-boot-starter 3.13.2 and Kotlin for cache but I get the following exception:
java.lang.IllegalArgumentException: java.io.NotSerializableException: com.service.message.State
at org.redisson.RedissonObject.encodeMapValue(RedissonObject.java:338)
at org.redisson.RedissonMapCache.fastPutOperationAsync(RedissonMapCache.java:843)
at org.redisson.RedissonMapCache.fastPutAsync(RedissonMapCache.java:746)
at org.redisson.RedissonMapCache.fastPut(RedissonMapCache.java:720)
at org.redisson.spring.cache.RedissonCache.put(RedissonCache.java:107)
at org.springframework.cache.interceptor.AbstractCacheInvoker.doPut(AbstractCacheInvoker.java:87)
at org.springframework.cache.interceptor.CacheAspectSupport$CachePutRequest.apply(CacheAspectSupport.java:820)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:429)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:345)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
at ...
Caused by: java.io.NotSerializableException: com.service.message.State
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:272)
at org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:58)
at org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:111)
at org.redisson.codec.MarshallingCodec$4.encode(MarshallingCodec.java:176)
at org.redisson.RedissonObject.encodeMapValue(RedissonObject.java:336)
... 115 common frames omitted
Caused by: org.jboss.marshalling.TraceInformation: null
This is how my config class looks like:
@Configuration
@EnableCaching
class CacheConfig {
@Bean
fun cacheManager(client: RedissonClient): CacheManager {
val config = org.redisson.spring.cache.CacheConfig()
config.ttl = 10 * 60 * 1000
return RedissonSpringCacheManager(client, mapOf("State" to config))
}
}
Type that I'm trying to cache:
data class State(
val phoneCode: String,
val phoneNumber: String
)
And this is how I'm trying to cache it:
@CachePut(value = ["State"], key = "#id")
fun initializeState(phoneAreaCode: String, phoneNumber: String, id: String): State {
...
return State(
phoneCode = "...",
phoneNumber = "...",
...
)
}
Edit: I managed to by pass this error by adding JsonJacksonCodec()
to RedissonSpringCacheManager(client, mapOf("State" to config), JsonJacksonCodec())
but now I'm having trouble deserializing:
unable to decode data. channel: [id: 0x81736df6, L:/...], reply: ReplayingDecoderByteBuf(ridx=1704, widx=1704), command: (EVAL), params: [local value = redis.call('hget', KEYS[1], ARGV[2]); if value == false then return nil; end; local t,..., 5, ...
com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class java.lang.Object]: missing type id property '@class'
at [Source: (io.netty.buffer.ByteBufInputStream); line: 1, column: 1695]
解决方案
我创建了一个特定的 Kotlin JsonJacksonCodec:
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator
import org.redisson.codec.JsonJacksonCodec
class JsonJacksonKotlinCodec(objectMapper: ObjectMapper) : JsonJacksonCodec(objectMapper) {
override fun initTypeInclusion(mapObjectMapper: ObjectMapper) {
mapObjectMapper.activateDefaultTyping(BasicPolymorphicTypeValidator.builder()
.allowIfBaseType(Any::class.java)
.build(), ObjectMapper.DefaultTyping.EVERYTHING)
}
}
此编解码器覆盖所有类型包含逻辑并包括所有内容的类型。数据类、密封类和一切正常。
要使用它:
val config = Config()
val codec = JsonJacksonKotlinCodec(ObjectMapper())
codec.objectMapper.registerKotlinModule()
config.codec = codec
config.useSingleServer().address = redisConfig.host
redissonClient = Redisson.create(config).reactive()
推荐阅读
- python - 为 tabWidget Python 导入负责的小部件页面
- java - 如何从番石榴重试器获取原始错误消息?
- python - Sklearn分类与DecisionTree,如何提高拟合?
- sql-server - 使用 sql repoting servives 创建多页、类似模板的报告
- c++ - C++ 循环中的可见性和布尔表达式
- python - 使用 Bash 脚本将超链接添加到所有第一列数据
- python - python同时运行两个while循环
- python - 使用第二个字典中的多个键更新嵌套字典
- c++ - 我如何获得0s海洋中的连续块数
- reactjs - 我希望我的 h4、h2 和 p 标签在我单击它们时更改输入