Handling the problem of spinning boot2 redis

1. Type conversion

Because the json serialization I used is saved to redis, the cast will fail when I get the SET collection

Object can't convert the entity class you write. The way to do this is to add 20% conversion

/**
	 *Get group users
	 * @param groupID
	 * @return
	 */
	public static Set<ChartUser> getGroupUser(ChatGroup groupID){
		return (Set<ChartUser>)(Set<?>)redisService.getSet(RedisPre.DB_GROUP_SET+groupID.getGroupCode());
	}

2. When reading redis, because I am using json string, there may be no deserialization data

Here is your serialization tool to adjust whether all fields exist before deserialization

3. Some of the data I want to serialize here can filter out some fields

I haven't thought of a good way yet. Two connections are used here (for example a Normal all fields, b Only some key fields)

//The version of jackson 2.10. Is used here. Other methods for finding vulnerabilities on the Internet have been disabled

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        // Configure redisTemplate
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer); // key serialization
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // value serialization
        redisTemplate.setHashKeySerializer(stringSerializer); // Hash key serialization
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); // Hash value serialization
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
    
    
    @Bean
    public RedisTemplate<String, Object> redisTemplateGroup(RedisConnectionFactory redisConnectionFactory) {
        // Configure redisTemplate
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer); // key serialization
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer(getMapper())); // value serialization
        redisTemplate.setHashKeySerializer(stringSerializer); // Hash key serialization
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer(getMapper())); // Hash value serialization
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
    
 // Get JSON tools
   	private final ObjectMapper getMapper() {
   		ObjectMapper mapper = new ObjectMapper();
   		JsonFilterUtil.addFilterForMapper(mapper);
   		//Serializing the class name into the json string will result in LinkedHashMap object, and direct conversion of entity object will fail
   		mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL); 	    //Ignore the properties existing in JSON string but not in Java object when setting input
   		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
   		return mapper;
   	}
}

Tags: Programming JSON Redis Java

Posted on Sat, 23 May 2020 11:25:13 -0400 by jfugate