当前位置: 代码迷 >> 综合 >> StringRedisTemplate操作Redis时抛: Unexpected token (VALUE_STRING)
  详细解决方案

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

热度:43   发布时间:2024-02-28 08:23:51.0

用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); 注释掉.

配置文件
 

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

 

  相关解决方案