Caused by: 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: (byte[])"{"name":"测试","phone":"123","idCard":"12345","channelCode":"0"}"; line: 1, column: 66]
\### Original Code: ```
@Autowired
private RedisTemplate<string string=""> redisTemplate;
redisTemplate.opsForValue().set(token, JSONUtil.toJsonStr(currentUserInfo), tokenExpireTime, TimeUnit.SECONDS);
String userInfoRaw = redisTemplate.opsForValue().get(token);
</string>
### Error Analysis: Redis stores objects with an additional @class property to identify the object type during deserialization. How ever, using JSONUtil.toJsonStr from Hutool to serialize objects removes the @class information. This results in a scenario where data can be stored but not properly retrieved from Redis. ### Solution: There are multiple approahces to resolve this issue: 1. Store the Object Directly: Allow Redis to handle the serialization process. 2. Use a Serializer That Preserves @class Information: Ensure the type information is included during serialization. 3. Retrieve Data as String or Map and Parse Manually: Handle the deserialization process separately. ### Modified Code: ```
@Autowired
private RedisTemplate redisTemplate;
redisTemplate.opsForValue().set(token, currentUserInfo, tokenExpireTime, TimeUnit.SECONDS);
Object userInfoRaw = redisTemplate.opsForValue().get(token);
\### Additional Solution for **LocalDateTime** Deserialization Errors: To fix issues related to deserializing **LocalDateTime** fields, configure the RedisTemplate with a custom **ObjectMapper**: ```
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<string object=""> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<string object=""> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(),
ObjectMapper.DefaultTyping.NON_FINAL,
JsonTypeInfo.As.PROPERTY);
// Configure date/time serialization/deserialization
JavaTimeModule timeModule = new JavaTimeModule();
timeModule.addDeserializer(LocalDate.class,
new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
timeModule.addDeserializer(LocalDateTime.class,
new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
timeModule.addSerializer(LocalDate.class,
new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
timeModule.addSerializer(LocalDateTime.class,
new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.registerModule(timeModule);
// Configure key serializers
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
// Configure value serializers
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer =
new GenericJackson2JsonRedisSerializer(objectMapper);
redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
</string></string>
This configuration ensures proper handling of object types and date/time formats when storing and retrieving data from Redis.