AppleDevApp
AppleDevApp

Reputation: 31

springboot redistemplate value has \x00 data

I use springboot 1.5.9 and redis-template. But when I save data to redis,I found that the value is wrong. Look like this:

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,\"name\":\"RandomName1512028732904\",\"salary\":12.34},{\"id\":2,\"name\":\"RandomName1512028735366\",\"salary\":12.34},{\"id\":3,\"name\":\"RandomName1512028738439\",\"salary\":12.34},{\"id\":4,\"name\":\"RandomName1512028750450\",\"salary\":12.34},{\"id\":5,\"name\":\"RandomName1512031361305\",\"salary\":12.34},{\"id\":6,\"name\":\"RandomName1512031361972\",\"salary\":12.34},{\"id\":7,\"name\":\"1512116645365\",\"salary\":12.34}]"
redisTemplate.opsForValue().set("indexCache", data, 10000);

How can I save the pure json to redis???

@Configuration
public class 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;
    }
}

Upvotes: 1

Views: 533

Answers (2)

Fred Fu
Fred Fu

Reputation: 21

There are multiple methods named set with different params, but maybe you used the set by starting at specified offset as below,

/**
 * 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);

I think the right method you would like to use would be

/**
 * 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);

so you could not emit param TimeUnit otherwise you will hit the problem you raised.

Upvotes: 1

AppleDevApp
AppleDevApp

Reputation: 31

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

Actually,this method has four parameters.

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

Upvotes: 1

Related Questions