@Configuration
@EnableCaching
public class RedisConfig {
@Bean
public RedisTemplate
<String, String> redisTemplate(RedisConnectionFactory factory
){
RedisTemplate
<String, String> redisTemplate
= new RedisTemplate<String,String>();
redisTemplate
.setConnectionFactory(factory
);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer
= new Jackson2JsonRedisSerializer(Object
.class);
ObjectMapper om
= new ObjectMapper();
om
.setVisibility(PropertyAccessor
.ALL
, JsonAutoDetect
.Visibility
.ANY
);
om
.enableDefaultTyping(ObjectMapper
.DefaultTyping
.NON_FINAL
);
jackson2JsonRedisSerializer
.setObjectMapper(om
);
StringRedisSerializer stringRedisSerializer
= new StringRedisSerializer();
redisTemplate
.setKeySerializer(stringRedisSerializer
);
redisTemplate
.setValueSerializer(stringRedisSerializer
);
redisTemplate
.setHashKeySerializer(stringRedisSerializer
);
redisTemplate
.setHashValueSerializer(jackson2JsonRedisSerializer
);
redisTemplate
.afterPropertiesSet();
return redisTemplate
;
}
}
使用Jackson2JsonRedisSerializer这个类就可以啦