StringRedisTemplate操作Redis时抛: Unexpected token (VALUE_STRING)

Use spring to StringRedisTemplateoperate Redis's list structure. It was normal when it was saved, and the following exception was reported when the number was retrieved. The exception has been resolved, but the principle of confusion. I don't know if I have encountered it, and know the detailed reason?

Abnormal error

`Unexpected token (VALUE_STRING), expected START_ARRAY: need JSON Array to contain As.WRAPPER_ARRAY type information for class java.util.Date`

solution
 

将配置类的 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 注释掉.

Configuration file
 

@Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 注释这行配置, 解决 `Unexpected token (VALUE_STRING), expected START_ARRAY: need JSON Array to contain As.WRAPPER_ARRAY type information for class java.util.Date`
//        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

 

Guess you like

Origin blog.csdn.net/CharlesYooSky/article/details/109071284