Java Code Examples for org.springframework.data.redis.core.RedisTemplate#setDefaultSerializer()

The following examples show how to use org.springframework.data.redis.core.RedisTemplate#setDefaultSerializer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedisAutoConfig.java    From yue-library with Apache License 2.0 6 votes vote down vote up
/**
 * <p>支持FastJson进行Redis存储对象序列/反序列化
 * <p>https://github.com/alibaba/fastjson/wiki/%E5%9C%A8-Spring-%E4%B8%AD%E9%9B%86%E6%88%90-Fastjson
 */
@Bean
public RedisTemplate<String, Object> yueRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
	RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
	redisTemplate.setConnectionFactory(redisConnectionFactory);
	
	// 支持FastJson进行Redis存储对象序列/反序列化
	if (redisProperties.getRedisSerializerEnum() != RedisSerializerEnum.JDK) {
		redisTemplate.setDefaultSerializer(redisProperties.getRedisSerializerEnum().getRedisSerializer());
	}
	StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
	redisTemplate.setKeySerializer(stringRedisSerializer);
	redisTemplate.setHashKeySerializer(stringRedisSerializer);
	
	return redisTemplate;
}
 
Example 2
Source File: RedisCacheAutoConfiguration.java    From open-cloud with MIT License 6 votes vote down vote up
/**
 * 重新配置一个RedisTemplate
 *
 * @param factory
 * @return
 */
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
    template.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);
    RedisSerializer<String> stringSerializer = new StringRedisSerializer();
    // key采用String的序列化方式
    template.setKeySerializer(stringSerializer);
    // hash的key也采用String的序列化方式
    template.setHashKeySerializer(stringSerializer);
    // value序列化方式采用jackson
    template.setValueSerializer(jackson2JsonRedisSerializer);
    // hash的value序列化方式采用jackson
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.setDefaultSerializer(jackson2JsonRedisSerializer);
    return template;
}
 
Example 3
Source File: RedisCacheConfig.java    From ywh-frame with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 覆盖默认的配置
 * @return RedisTemplate
 */
@Bean
public RedisTemplate<String,Object> redisTemplate(){
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);
    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);


    // 设置value的序列化规则和key的序列化规则
    template.setKeySerializer(stringRedisSerializer);
    template.setValueSerializer(fastJsonRedisSerializer);
    template.setHashKeySerializer(stringRedisSerializer);
    template.setHashValueSerializer(fastJsonRedisSerializer);
    template.setDefaultSerializer(fastJsonRedisSerializer);
    template.afterPropertiesSet();
    return template;
}
 
Example 4
Source File: RedisConfig.java    From DimpleBlog with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    // set key serializer
    StringRedisSerializer serializer = MyRedisCacheManager.STRING_SERIALIZER;
    template.setKeySerializer(serializer);
    template.setHashKeySerializer(serializer);

    GenericFastJsonRedisSerializer fastSerializer = MyRedisCacheManager.FASTJSON_SERIALIZER;
    template.setValueSerializer(fastSerializer);
    template.setHashValueSerializer(fastSerializer);
    template.setDefaultSerializer(fastSerializer);

    template.setConnectionFactory(connectionFactory);
    template.afterPropertiesSet();
    return template;
}
 
Example 5
Source File: JsonRedisSerializable.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
/**
*  配置Redis
* @param redisConnectionFactory redis连接工厂
*/
  @Bean
  @ConditionalOnMissingBean(name = "redisTemplate")
  public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
      RedisTemplate<Object, Object> template = new RedisTemplate<>();
      template.setConnectionFactory(redisConnectionFactory);
      template.setDefaultSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
      return template;
  }
 
Example 6
Source File: RedisConfiguration.java    From sanshanblog with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
    RedisTemplate<String, String> redis = new RedisTemplate<String, String>();
    GenericFastJsonRedisSerializer fastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
    redis.setDefaultSerializer(fastJsonRedisSerializer);
    redis.setConnectionFactory(cf);
    redis.afterPropertiesSet();
    return redis;
}
 
Example 7
Source File: RedisCacheConfig.java    From sanshanblog with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
    RedisTemplate<String, String> redis = new RedisTemplate<String, String>();
    GenericFastJsonRedisSerializer fastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
    redis.setDefaultSerializer(fastJsonRedisSerializer);
    redis.setConnectionFactory(cf);
    redis.afterPropertiesSet();
    return redis;
}
 
Example 8
Source File: ChoerodonRedisHttpSessionConfiguration.java    From oauth-server with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisTemplate<Object, Object> sessionRedisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setDefaultSerializer(customJdkSerializationRedisSerializer());
    template.setConnectionFactory(connectionFactory);
    return template;
}
 
Example 9
Source File: J2CacheSpringRedisAutoConfiguration.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Bean("j2CacheRedisTemplate")
public RedisTemplate<String, Serializable> j2CacheRedisTemplate(
		@Qualifier("j2CahceRedisConnectionFactory") RedisConnectionFactory j2CahceRedisConnectionFactory,
		@Qualifier("j2CacheValueSerializer") RedisSerializer<Object> j2CacheSerializer) {
	RedisTemplate<String, Serializable> template = new RedisTemplate<String, Serializable>();
	template.setKeySerializer(new StringRedisSerializer());
	template.setHashKeySerializer(new StringRedisSerializer());
	template.setDefaultSerializer(j2CacheSerializer);
	template.setConnectionFactory(j2CahceRedisConnectionFactory);
	return template;
}
 
Example 10
Source File: J2CacheSpringRedisAutoConfiguration.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Bean("j2CacheRedisTemplate")
@ConditionalOnBean(name = "j2CahceRedisConnectionFactory")
public RedisTemplate<String, Serializable> j2CacheRedisTemplate(
		@Qualifier("j2CahceRedisConnectionFactory") JedisConnectionFactory j2CahceRedisConnectionFactory) {
	RedisTemplate<String, Serializable> template = new RedisTemplate<String, Serializable>();
	template.setKeySerializer(new StringRedisSerializer());
	template.setHashKeySerializer(new StringRedisSerializer());
	template.setDefaultSerializer(new J2CacheSerializer());
	template.setConnectionFactory(j2CahceRedisConnectionFactory);
	return template;
}
 
Example 11
Source File: JsonRedisSerializable.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
/**
*  配置Redis
* @param redisConnectionFactory redis连接工厂
*/
  @Bean
  @ConditionalOnMissingBean(name = "redisTemplate")
  public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
      RedisTemplate<Object, Object> template = new RedisTemplate<>();
      template.setConnectionFactory(redisConnectionFactory);
      template.setDefaultSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
      return template;
  }
 
Example 12
Source File: RedisConfig.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 配置自定义Json序列化器 jackson2
 *
 * @param redisConnectionFactory :redis连接工厂
 * @return :RedisTemplate
 */
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);
    // 使用jackson2序列化
    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);

    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    mapper.registerModule(new Jdk8Module())
            .registerModule(new JavaTimeModule());
    jackson2JsonRedisSerializer.setObjectMapper(mapper);
    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

    // key采用String的序列化方式
    template.setKeySerializer(stringRedisSerializer);
    // hash的key也采用String的序列化方式
    template.setHashKeySerializer(stringRedisSerializer);
    // value序列化方式采用jackson
    template.setValueSerializer(jackson2JsonRedisSerializer);

    // hash的value序列化方式采用jackson
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();


    // 设置默认使用Jackson序列化
    template.setDefaultSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
    return template;
}
 
Example 13
Source File: RedisHttpSessionConfiguration.java    From spring-session with Apache License 2.0 5 votes vote down vote up
private RedisTemplate<Object, Object> createRedisTemplate() {
	RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
	redisTemplate.setKeySerializer(new StringRedisSerializer());
	redisTemplate.setHashKeySerializer(new StringRedisSerializer());
	if (this.defaultRedisSerializer != null) {
		redisTemplate.setDefaultSerializer(this.defaultRedisSerializer);
	}
	redisTemplate.setConnectionFactory(this.redisConnectionFactory);
	redisTemplate.setBeanClassLoader(this.classLoader);
	redisTemplate.afterPropertiesSet();
	return redisTemplate;
}
 
Example 14
Source File: J2CacheSpringRedisAutoConfiguration.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Bean("j2CacheRedisTemplate")
public RedisTemplate<String, Serializable> j2CacheRedisTemplate(
    @Qualifier("j2CahceRedisConnectionFactory") RedisConnectionFactory j2CahceRedisConnectionFactory,
    @Qualifier("j2CacheValueSerializer") RedisSerializer<Object> j2CacheSerializer) {
    RedisTemplate<String, Serializable> template = new RedisTemplate<String, Serializable>();
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setDefaultSerializer(j2CacheSerializer);
    template.setConnectionFactory(j2CahceRedisConnectionFactory);
    return template;
}
 
Example 15
Source File: RedisConfig.java    From AnyMock with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisTemplate<String, HttpInterfaceBO> httpInterfaceRedisTemplate() {
    RedisTemplate<String, HttpInterfaceBO> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(redisConnectionFactory);
    redisTemplate.setDefaultSerializer(new FastJsonRedisSerializer<>(String.class));
    redisTemplate.setValueSerializer(new FastJsonRedisSerializer<>(HttpInterfaceBO.class));
    return redisTemplate;
}
 
Example 16
Source File: MyRedisConfig.java    From code with Apache License 2.0 5 votes vote down vote up
/**
 * 自定义 RedisTemplate 序列化器
 */
@Bean
public RedisTemplate<Object, Department> deptRedisTemplate(
        RedisConnectionFactory redisConnectionFactory)
        throws UnknownHostException {
    RedisTemplate<Object, Department> template = new RedisTemplate<Object, Department>();
    template.setConnectionFactory(redisConnectionFactory);
    Jackson2JsonRedisSerializer<Department> ser = new Jackson2JsonRedisSerializer<Department>(Department.class);
    template.setDefaultSerializer(ser);
    return template;
}
 
Example 17
Source File: MyRedisConfig.java    From code with Apache License 2.0 5 votes vote down vote up
/**
 * 自定义 RedisTemplate 序列化器
 */
@Bean
public RedisTemplate<Object, Employee> empRedisTemplate(
        RedisConnectionFactory redisConnectionFactory)
        throws UnknownHostException {
    RedisTemplate<Object, Employee> template = new RedisTemplate<Object, Employee>();
    template.setConnectionFactory(redisConnectionFactory);
    Jackson2JsonRedisSerializer<Employee> ser = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
    template.setDefaultSerializer(ser);
    return template;
}
 
Example 18
Source File: RedisConfig.java    From codeway_service with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 配置自定义Json序列化器 jackson2
 *
 * @param redisConnectionFactory :redis连接工厂
 * @return :RedisTemplate
 */
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);
    // 使用jackson2序列化
    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);

    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    mapper.registerModule(new Jdk8Module())
            .registerModule(new JavaTimeModule());
    jackson2JsonRedisSerializer.setObjectMapper(mapper);
    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

    // key采用String的序列化方式
    template.setKeySerializer(stringRedisSerializer);
    // hash的key也采用String的序列化方式
    template.setHashKeySerializer(stringRedisSerializer);
    // value序列化方式采用jackson
    template.setValueSerializer(jackson2JsonRedisSerializer);

    // hash的value序列化方式采用jackson
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();


    // 设置默认使用Jackson序列化
    template.setDefaultSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
    return template;
}
 
Example 19
Source File: RedisBeansConfig.java    From tac with MIT License 4 votes vote down vote up
public static RedisTemplate getRedisTemplate(JedisConnectionFactory jedisConnectionFactory) {
    RedisTemplate redisTemplate = new RedisTemplate();
    redisTemplate.setConnectionFactory(jedisConnectionFactory);
    redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
    return redisTemplate;
}
 
Example 20
Source File: RedisConfiguration.java    From spring-boot-with-multi-redis with MIT License 4 votes vote down vote up
private void setSerializer(RedisTemplate<String, String> template) {
    template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
}