Solve Jackson 2 deserialization LocalDateTime error

Today, when integrating redis and spring boot, I encountered an error. Record it. The error is reported as follows: Could not read JSON: Cannot constr...

Today, when integrating redis and spring boot, I encountered an error. Record it.

The error is reported as follows:

Could not read JSON: Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

After viewing, it is found that when the data is taken out of redis, Jackson 2 cannot process the LocalDateTime type by deserializing the data. The reason is that when Jackson 2 serializes the LocalDateTime, it is not in the normal time format, but in the format shown in the following figure

{ "date": { "year": 2019, "month": "FEBRUARY", "day": 27, "dayOfMonth": 27, "dayOfWeek": "WEDNESDAY", "era": [ "java.time.chrono.IsoEra", "CE" ], "dayOfYear": 58, "leapYear": false, "chronology": { "id": "ISO", "calendarType": "iso8601" }, "prolepticMonth": 24229, "monthValue": 2 }, "time": { "hour": 12, "minute": 10, "second": 17, "nano": 0 }, "month": "FEBRUARY", "year": 2019, "dayOfMonth": 27, "dayOfWeek": "WEDNESDAY", "dayOfYear": 58, "hour": 12, "minute": 10, "nano": 0, "second": 17, "monthValue": 2, "chronology": [ "java.time.chrono.IsoChronology", { "id": "ISO", "calendarType": "iso8601" } ] }

Therefore, the serialization of LocalDateTime by Jackson 2 is not consistent with what we expected. Just modify the serialization template registered to redis as follows

1 @Configuration 2 public class RedisConfig { 3 4 /** 5 * Define UserRedisTemplate, specify the processing classes of serialization and deserialization 6 * 7 * @param factory redis Connection factory 8 * @return Template 9 */ 10 @Bean("UserRedisTemplate") 11 public RedisTemplate<String, User> userRedisTemplate(RedisConnectionFactory factory) { 12 RedisTemplate<String, User> template = new RedisTemplate<>(); 13 template.setConnectionFactory(factory); 14 Jackson2JsonRedisSerializer<User> j2jrs = new Jackson2JsonRedisSerializer<>(User.class); 15 ObjectMapper om = new ObjectMapper(); 16 om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 17 // Solve the problem that jackson2 cannot deserialize LocalDateTime 18 om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); 19 om.registerModule(new JavaTimeModule()); 20 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); 21 j2jrs.setObjectMapper(om); 22 // serialize value Use this serialization method when 23 template.setValueSerializer(j2jrs); 24 template.setHashValueSerializer(j2jrs); 25 // serialize key Time 26 StringRedisSerializer srs = new StringRedisSerializer(); 27 template.setKeySerializer(srs); 28 template.setHashKeySerializer(srs); 29 template.afterPropertiesSet(); 30 return template; 31 } 32 }

Mainly the two sentences marked with red

Reference documents:

Deserialization of GenericJackson2JsonRedisSerializer

2 December 2019, 01:51 | Views: 3431

Add new comment

For adding a comment, please log in
or create account

0 comments