Redis serialization and deserialization are inconsistent-String type value has more double quotes problem

Problem background:

The A service writes the data to Redis, and the B service reads out the value with double quotes. For example, "String" gets ""String"".

problem causes:

A service adds a redisTemplate Bean configuration:

@Configuration
public class RedisTemplateConfig {
    @Bean(name = "redisTemplate")
    public RedisTemplate setRedisTemplate(RedisConnectionFactory redisConnectionFactory, RedisProperties redisProperties) {
        StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        redisProperties.setPassword(SecretKeyClient.getPassword(
                System.getProperty("datakeeper.application.redis.community.key_name")));
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

We can see that ValueSerializer uses jackson2JsonRedisSerializer.
When used, it is introduced through the @Resource annotation:

    @Resource
    private RedisTemplate<String, String> redisTemplate;

@Resource is injected through beanName by default, so the RedisTemplate injected at this time is what we configured above.

In service B:
A RedisTemplate is also configured:

@Configuration
public class RedisTemplateConfig {
    @Bean(name = "redisTemplate")
    public RedisTemplate setRedisTemplate(RedisConnectionFactory redisConnectionFactory, RedisProperties redisProperties) {
        StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        redisProperties.setPassword(SecretKeyClient.getPassword(
                System.getProperty("datakeeper.application.redis.community.key_name")));
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

The configuration is exactly the same as the A service.
However, the @Autowired annotation used when using RedisTemplate:

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

We know that the @Autowired annotation is injected according to BeanClass or BeanType by default. At this time, the injected RedisTemplate is not configured by us above, but automatically configured by SpringBoot.
In RedisAutoConfiguration:

	@Bean
	@ConditionalOnMissingBean(name = "redisTemplate")
	public RedisTemplate<Object, Object> redisTemplate(
			RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
		RedisTemplate<Object, Object> template = new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}

We see that our configuration is different from the default redisTemplate of SpringBoot.

Compare by breaking point:
SpringBoot default:

Insert picture description here


Our own definition: As

Insert picture description here


you can see, on the valueSerializer, one is StringRedisSerializer and the other is Jackson2JsonRedisSerializer.
Therefore, when the serialization and deserialization methods are different, this kind of garbled code occurs, and the strange double quote problem can be explained.

solution

Change the serialization and deserialization method to the same.