数据库
首页 > 数据库> > redis出现序列化问题

redis出现序列化问题

作者:互联网

问题:

存在redis里面的内容有日期,但是存进去的格式不对。

1  @Cacheable(value = "banner", key = "'selectIndexList'")
2     @ApiOperation(value = "获取首页banner")
3     @GetMapping4     public R index() {
5         List<CmsBanner> list = bannerService.selectIndexList();
6         return R.ok().data("bannerList", list);
7     }

 

 1 Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 2  at [Source: (byte[])"["com.atguigu.commonutils.R",{"success":true,"code":20000,"message":"成功","data":["java.util.HashMap",{"bannerList":["java.util.ArrayList",[["com.atguigu.cmsservice.entity.CmsBanner",{"id":"1194607458461216770","title":"Golang","typeId":"","imageUrl":"https://my-online-education-project.oss-cn-beijing.aliyuncs.com/2021/05/03/3dc5bbacc2ea4f9fa8e1cf9e73ab4f0cgo.png","color":null,"linkUrl":"/course/1389184259991093249","sort":0,"gmtCreate":{"date":{"year":2021,"month":"MAY","day":4,"dayOfMonth":"[truncated 2351 bytes]; line: 1, column: 446] (through reference chain: com.atguigu.commonutils.R["data"]->java.util.HashMap["bannerList"]->java.util.ArrayList[0]->com.atguigu.cmsservice.entity.CmsBanner["gmtCreate"])
 3     at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
 4     at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1589)
 5     at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1055)
 6     at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1297)
 7     at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326)
 8     at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
 9     at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
10     at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
11     at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
12     at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:120)
13     at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromAny(AsArrayTypeDeserializer.java:71)
14     at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserializeWithType(UntypedObjectDeserializer.java:712)
15     at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:288)
16     at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
17     at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
18     at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:120)
19     at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromAny(AsArrayTypeDeserializer.java:71)
20     at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserializeWithType(UntypedObjectDeserializer.java:712)
21     at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:529)
22     at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:364)
23     at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:29)
24     at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:120)
25     at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromObject(AsArrayTypeDeserializer.java:61)
26     at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserializeWithType(MapDeserializer.java:400)
27     at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:138)
28     at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
29     at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
30     at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:120)
31     at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromAny(AsArrayTypeDeserializer.java:71)
32     at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserializeWithType(UntypedObjectDeserializer.java:712)
33     at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:68)
34     at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4202)
35     at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3309)
36     at org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer.deserialize(Jackson2JsonRedisSerializer.java:73)
37     ... 65 more

redis里面内容:

"[\"com.atguigu.commonutils.R\",{\"success\":true,\"code\":20000,\"message\":\"\xe6\x88\x90\xe5\x8a\x9f\",\"data\":[\"java.util.HashMap\",{\"bannerList\":[\"java.util.ArrayList\",[[\"com.atguigu.cmsservice.entity.CmsBanner\",{\"id\":\"1194607458461216770\",\"title\":\"Golang\",\"typeId\":\"\",\"imageUrl\":\"https://my-online-education-project.oss-cn-beijing.aliyuncs.com/2021/05/03/3dc5bbacc2ea4f9fa8e1cf9e73ab4f0cgo.png\",\"color\":null,\"linkUrl\":\"/course/1389184259991093249\",\"sort\":0,\"gmtCreate\":{\"date\":{\"year\":2021,\"month\":\"MAY\",\"day\":4,\"dayOfMonth\":4,\"monthValue\":5,\"chronology\":{\"id\":\"ISO\",\"calendarType\":\"iso8601\"},\"dayOfWeek\":\"TUESDAY\",\"era\":[\"java.time.chrono.IsoEra\",\"CE\"],\"dayOfYear\":124,\"leapYear\":false,\"prolepticMonth\":24256},\"time\":{\"hour\":1,\"minute\":20,\"second\":50,\"nano\":0},\"month\":\"MAY\",\"year\":2021,\"dayOfMonth\":4,\"hour\":1,\"minute\":20,\"monthValue\":5,\"nano\":0,\"second\":50,\"dayOfWeek\":\"TUESDAY\",\"dayOfYear\":124,\"chronology\":[\"java.time.chrono.IsoChronology\",{\"id\":\"ISO\",\"calendarType\":\"iso8601\"}]},\"gmtModified\":{\"date\":{\"year\":2021,\"month\":\"MAY\",\"day\":4,\"dayOfMonth\":4,\"monthValue\":5,\"chronology\":{\"id\":\"ISO\",\"calendarType\":\"iso8601\"},\"dayOfWeek\":\"TUESDAY\",\"era\":[\"java.time.chrono.IsoEra\",\"CE\"],\"dayOfYear\":124,\"leapYear\":false,\"prolepticMonth\":24256},\"time\":{\"hour\":1,\"minute\":20,\"second\":52,\"nano\":0},\"month\":\"MAY\",\"year\":2021,\"dayOfMonth\":4,\"hour\":1,\"minute\":20,\"monthValue\":5,\"nano\":0,\"second\":52,\"dayOfWeek\":\"TUESDAY\",\"dayOfYear\":124,\"chronology\":[\"java.time.chrono.IsoChronology\",{\"id\":\"ISO\",\"calendarType\":\"iso8601\"}]}}],[\"com.atguigu.cmsservice.entity.CmsBanner\",{\"id\":\"1194607458461216769\",\"title\":\"C++\",\"typeId\":\"\",\"imageUrl\":\"https://my-online-education-project.oss-cn-beijing.aliyuncs.com/2021/05/03/e02259dcfb244936a9ecae3db3377970safjkalsfj.jpg\",\"color\":null,\"linkUrl\":\"/course/1389181626911899650\",\"sort\":2,\"gmtCreate\":{\"date\":{\"year\":2021,\"month\":\"MAY\",\"day\":4,\"dayOfMonth\":4,\"monthValue\":5,\"chronology\":{\"id\":\"ISO\",\"calendarType\":\"iso8601\"},\"dayOfWeek\":\"TUESDAY\",\"era\":[\"java.time.chrono.IsoEra\",\"CE\"],\"dayOfYear\":124,\"leapYear\":false,\"prolepticMonth\":24256},\"time\":{\"hour\":1,\"minute\":19,\"second\":17,\"nano\":0},\"month\":\"MAY\",\"year\":2021,\"dayOfMonth\":4,\"hour\":1,\"minute\":19,\"monthValue\":5,\"nano\":0,\"second\":17,\"dayOfWeek\":\"TUESDAY\",\"dayOfYear\":124,\"chronology\":[\"java.time.chrono.IsoChronology\",{\"id\":\"ISO\",\"calendarType\":\"iso8601\"}]},\"gmtModified\":{\"date\":{\"year\":2021,\"month\":\"MAY\",\"day\":4,\"dayOfMonth\":4,\"monthValue\":5,\"chronology\":{\"id\":\"ISO\",\"calendarType\":\"iso8601\"},\"dayOfWeek\":\"TUESDAY\",\"era\":[\"java.time.chrono.IsoEra\",\"CE\"],\"dayOfYear\":124,\"leapYear\":false,\"prolepticMonth\":24256},\"time\":{\"hour\":1,\"minute\":19,\"second\":20,\"nano\":0},\"month\":\"MAY\",\"year\":2021,\"dayOfMonth\":4,\"hour\":1,\"minute\":19,\"monthValue\":5,\"nano\":0,\"second\":20,\"dayOfWeek\":\"TUESDAY\",\"dayOfYear\":124,\"chronology\":[\"java.time.chrono.IsoChronology\",{\"id\":\"ISO\",\"calendarType\":\"iso8601\"}]}}]]]}]}]

 

可以看出来应该是序列化日期对象的时候失败了。

 

解决:

注入如下的bean对象

 1     @Bean(name = "mapperObject")
 2     public ObjectMapper getObjectMapper() {
 3         ObjectMapper om = new ObjectMapper();
 4         JavaTimeModule javaTimeModule = new JavaTimeModule();
 5         javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
 6         javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
 7         javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
 8         om.registerModule(javaTimeModule);
 9         return om;
10     }

再在实体类的时间属性上面加上以下注解

@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime gmtModified;

 

标签:deser,java,redis,databind,问题,jackson,序列化,com,fasterxml
来源: https://www.cnblogs.com/hong-yf/p/16455835.html