How to solve the garbled code of @ Cacheable cache value?

RedisTemplate has been configured

@Bean
public RedisTemplate redisTemplateInit() {
    //Key
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    //Value
    redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    return redisTemplate;
}

there is no problem setting values directly through RedisTemplate now.

but by caching data using @ Cacheable annotations on the method, key is normal and value will show some garbled code, as follows:
"xacxedx00x05tx00x1fStudent {name="test", age=22}"

is garbled in redis, and it is normal to take it directly with code

.

has anyone encountered this problem? How to solve

Mar.28,2021

is it the second time to request the interface to garbled the returned data?

is still garbled when querying values in redis.

I also tested it in a small project in springboot, but it worked fine.

jpa:
    show-sql: true
  jackson:
    default-property-inclusion: non_null
  redis:
    host: 192.168.1.111
    port: 6379

it is normal for AOP that uses StringRedisTemplate to store token. No, I did not configure anything, but directly used the integration of springboot.

import org.springframework.data.redis.core.StringRedisTemplate;

then I added @ Cacheable to cache the product information when I asked for the list of the item.

@GetMapping("/list")
    @Cacheable(cacheNames = "product",key = "1")
//    @Cacheable(cacheNames = "product", key = "-sharpsellerId", condition = "-sharpsellerId.length() > 3", unless = "-sharpresult.getCode() != 0")
    public ResultVO list(){
        
        ...
        ...
        ...
        return ResultVOUtil.success(productVOList);
    }

both the page and postman display data normally, and the console also runs out of sql (I perform two sql operations here)


sqlredis
redis

redis

can you tell me more about your bug problem and reproduce the environment in which it happened?


    
  
`
//@Cacheable value 
@Bean  
public CacheManager cacheManager(RedisConnectionFactory factory) {  
    RedisSerializer<String> redisSerializer = new StringRedisSerializer();  
    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);  
  
    //   
  RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()  
            .entryTtl(Duration.ZERO)//  
  .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))  
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))  
            .disableCachingNullValues();  
  
    RedisCacheManager cacheManager = RedisCacheManager.builder(factory)  
            .cacheDefaults(config)  
            .build();  
    return cacheManager;  
}
//value 
//  redisTemplate.opsForValue().set("wang","");
@Bean  
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {  
    //  
  //Jackson2JsonRedisSerializerredisvalueJDK  
  Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);  
    ObjectMapper om = new ObjectMapper();  
    //field,getset,ANYprivatepublic  
  om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);  
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON\_FINAL);  
    jackson2JsonRedisSerializer.setObjectMapper(om);  
    //redisTemplate  
  RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();  
    //   
  redisTemplate.setConnectionFactory(lettuceConnectionFactory);  
    RedisSerializer stringSerializer = new StringRedisSerializer();  
    redisTemplate.setKeySerializer(stringSerializer);//key  
  redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);//value  
  redisTemplate.setHashKeySerializer(stringSerializer);//Hash key  
  redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);//Hash value  
  redisTemplate.afterPropertiesSet();  
    return redisTemplate;  
}
Menu