Springboot_StringRedisTemplate配置

@Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        //jackson将java对象转换成json对象。
        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);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }


//	<dependency>
//	<groupId>org.springframework.boot</groupId>
//	<artifactId>spring-boot-starter-redis</artifactId>
//	<version>1.3.1.RELEASE</version>
//	</dependency>
//	<!-- jackson-databind -->
//	<dependency>
//	<groupId>com.fasterxml.jackson.core</groupId>
//	<artifactId>jackson-databind</artifactId>
//	<version>2.9.5</version>
//	</dependency>
//	<dependency>
//	<groupId>com.fasterxml.jackson.core</groupId>
//	<artifactId>jackson-annotations</artifactId>
//	<version>2.9.5</version>
//	</dependency>

  

posted @ 2018-08-31 14:33  scwyfy  阅读(4466)  评论(0编辑  收藏  举报