springboot redistemplate值有\ x00数据

时间:2017-12-07 09:15:45

标签: spring-boot redis

我使用springboot 1.5.9和redis-template。 但是当我将数据保存到redis时,我发现值是错误的。 看起来像这样: 0 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 \ X00 [{\ “ID \”:1,\ “名称\”:\“RandomName1512028 732904 \”,\ “薪水\”:12.34},{\ “ID \”:2,\ “名称\”:\ “RandomName1512028735366 \”,\ “薪水\”:12.34},{\ “ID \”: 3,\ “名称\”:\ “RandomName1512028738439 \”,\ “薪水\”:12.34},{\ “ID \”:4,\ “名称\”:\ “RandomName1512028750450 \”,\ “薪水\”: 12.34},{\ “ID \”:5,\ “名称\”:\ “RandomName1512031361305 \”,\ “薪水\”:12.34},{\ “ID \”:6,\ “名称\”:\” RandomName1512031361972 \ “\ ”薪水\“:12.34},{\ ”ID \“:7,\ ”名称\“:\ ”1512116645365 \“,\ ”薪水\“:12.34}]”

redisTemplate.opsForValue()。set(“indexCache”,data,10000); 如何将纯json保存到redis ???

@Configuration 公共课RedisConfig {

@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(redisConnectionFactory);

    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    //objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

    jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
    redisTemplate.afterPropertiesSet();
    return redisTemplate;
}

}

2 个答案:

答案 0 :(得分:0)

redisTemplate.opsForValue().set("indexCache", data, 10000); 

实际上,这种方法有四个参数。

redisTemplate.opsForValue().set("indexCache", data, 10000,TimeUnits.Seconds); 

答案 1 :(得分:0)

有多个名为 set 的方法具有不同的参数,但也许您通过如下指定的偏移量开始使用 set,

/**
 * Overwrite parts of {@code key} starting at the specified {@code offset} with given {@code value}.
 *
 * @param key must not be {@literal null}.
 * @param value
 * @param offset
 * @see <a href="https://redis.io/commands/setrange">Redis Documentation: SETRANGE</a>
 */
void set(K key, V value, long offset);

我认为您想使用的正确方法是

/**
 * Set the {@code value} and expiration {@code timeout} for {@code key}.
 *
 * @param key must not be {@literal null}.
 * @param value must not be {@literal null}.
 * @param timeout the key expiration timeout.
 * @param unit must not be {@literal null}.
 * @see <a href="https://redis.io/commands/setex">Redis Documentation: SETEX</a>
 */
void set(K key, V value, long timeout, TimeUnit unit);

所以你不能发出参数 TimeUnit 否则你会遇到你提出的问题。

相关问题