StringRedisTemplate操作Redis時拋: Unexpected token (VALUE_STRING)

程式猿微刊發表於2020-10-14

用spring的StringRedisTemplate操作Redis的list結構. 存入時正常, 取數時報了下面的異常,異常已經解決, 但是疑惑原理. 不知道有沒有遇到過, 並且知道詳細原因的?

異常報錯

`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); 註釋掉.

配置檔案
 

@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;
    }

 

相關文章