1. redisTemplate 報錯:java
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (START_OBJECT), expected START_ARRAY: need JSON Array to contain As.WRAPPER_ARRAY type information for class java.lang.Object
2. 報錯代碼行:redis
Object dd = valueOperations.get(key);
3. 解決,找到配置類,註釋掉這一行:json
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
PS:對應配置方法完整代碼 :app
/** * retemplate相關配置 * * @param factory * @return */ @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); // 配置鏈接工廠 template.setConnectionFactory(factory); //使用Jackson2JsonRedisSerializer來序列化和反序列化redis的value值(默認使用JDK的序列化方式) Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); // 指定要序列化的域,field,get和set,以及修飾符範圍,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // 指定序列化輸入的類型,類必須是非final修飾的,final修飾的類,好比String,Integer等會跑出異常 // om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); // 保留這行會報錯:Unexpected token (VALUE_STRING) jacksonSeial.setObjectMapper(om); // 值採用json序列化 template.setValueSerializer(jacksonSeial); //使用StringRedisSerializer來序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); // 設置hash key 和value序列化模式 template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(jacksonSeial); template.afterPropertiesSet(); redisTemplate = template; // 注入失敗,故直接用屬性來獲取 return template; }