本文目录
一、背景描述
二、问题原因
三、解决方案
一、背景描述
Spring boot 项目集成 Alibaba 的 JetCache 缓存开源框架。
Spring boot 集成 JetCache 框架之后,在需要添加缓存的方法上添加注解(@Cached)即可。已开启 本地缓存 + 远程缓存。示例代码如下:
上述方法中的返回值是 JavaBean 类型的,而我项目中的 JavaBean 代码如下:
注意:这个 JavaBean 没有实现序列化接口(这里是重点哦)。
在程序给这个方法远程缓存时,系统报错如下:
com.alicp.jetcache.support.CacheEncodeException: Java Encode error. msg=com.iot.back.message.process.dto.DeviceBasicInfoDTO
at com.alicp.jetcache.support.JavaValueEncoder.apply(JavaValueEncoder.java:57)
at com.alicp.jetcache.support.JavaValueEncoder.apply(JavaValueEncoder.java:13)
at com.alicp.jetcache.redis.RedisCache.do_PUT(RedisCache.java:169)
at com.alicp.jetcache.AbstractCache.PUT(AbstractCache.java:242)
at com.alicp.jetcache.MultiLevelCache.PUT_caches(MultiLevelCache.java:193)
at com.alicp.jetcache.MultiLevelCache.do_PUT(MultiLevelCache.java:164)
at com.alicp.jetcache.AbstractCache.PUT(AbstractCache.java:242)
at com.alicp.jetcache.MultiLevelCache.PUT(MultiLevelCache.java:66)
at com.alicp.jetcache.Cache.put(Cache.java:74)
at com.alicp.jetcache.SimpleProxyCache.put(SimpleProxyCache.java:43)
at com.alicp.jetcache.AbstractCache.lambda$computeIfAbsentImpl$2(AbstractCache.java:153)
at com.alicp.jetcache.AbstractCache.computeIfAbsentImpl(AbstractCache.java:163)
at com.alicp.jetcache.RefreshCache.computeIfAbsent(RefreshCache.java:61)
at com.alicp.jetcache.RefreshCache.computeIfAbsent(RefreshCache.java:56)
at com.alicp.jetcache.anno.method.CacheHandler.invokeWithCached(CacheHandler.java:245)
at com.alicp.jetcache.anno.method.CacheHandler.doInvoke(CacheHandler.java:92)
at com.alicp.jetcache.anno.method.CacheHandler.invoke(CacheHandler.java:84)
at com.alicp.jetcache.anno.aop.JetCacheInterceptor.invoke(JetCacheInterceptor.java:74)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
at com.iot.back.message.process.rpc.DeviceBasicInfoRpc$$EnhancerBySpringCGLIB$$a4ce3e3e.getDeviceBasicInfo(<generated>)
at com.iot.back.message.process.domain.service.impl.DeviceReportServiceImpl.report2AlarmMessageDTO(DeviceReportServiceImpl.java:118)
at com.iot.back.message.process.domain.service.impl.DeviceReportServiceImpl.execute(DeviceReportServiceImpl.java:54)
at com.iot.back.message.process.apps.impl.ReportRouteServiceImpl.route(ReportRouteServiceImpl.java:56)
at com.iot.back.message.process.apps.impl.ReportMessageResolveImpl.report2log(ReportMessageResolveImpl.java:26)
at com.iot.back.message.process.kafka.listener.ReportConsumerListener.lambda$kafkaConsumer$0(ReportConsumerListener.java:36)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.NotSerializableException: com.iot.back.message.process.dto.DeviceBasicInfoDTO
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at com.alicp.jetcache.support.JavaValueEncoder.apply(JavaValueEncoder.java:48)
... 30 common frames omitted
二、问题原因
上述程序报错的原因是程序在给这个方法的返回值进行缓存时,需要先序列化这个 JavaBean,然而呢,我项目中的这个 JavaBean 并没有实现序列化接口(Serializable),所以在程序运行时就报了如下的错误。
三、解决方案
解决方法很简单,将用到的这个 JavaBean 实现 序列化接口即可。
?Bingo,重启程序可以正常运行啦。
完结!
|