diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisTemplate.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisTemplate.java index fdef739bf..bbc6d861c 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisTemplate.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisTemplate.java @@ -453,6 +453,19 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation return (T) values; } + @SuppressWarnings("unchecked") + private Collection deserializeHashKeys(Collection rawKeys, Class type) { + Collection values = (List.class.isAssignableFrom(type) ? new ArrayList(rawKeys.size()) + : new LinkedHashSet(rawKeys.size())); + for (byte[] bs : rawKeys) { + if (bs != null) { + values.add((H) hashKeySerializer.deserialize(bs)); + } + } + + return values; + } + @SuppressWarnings("unchecked") private Collection deserializeHashValues(Collection rawValues, Class type) { Collection values = (List.class.isAssignableFrom(type) ? new ArrayList(rawValues.size()) @@ -1749,7 +1762,7 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation } }, true); - return (Set) deserializeHashValues(rawValues, Set.class); + return (Set) deserializeHashKeys(rawValues, Set.class); } @Override diff --git a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/support/collections/RedisMapTests.java b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/support/collections/RedisMapTests.java index 166c737dd..462b0669f 100644 --- a/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/support/collections/RedisMapTests.java +++ b/spring-data-redis/src/test/java/org/springframework/data/keyvalue/redis/support/collections/RedisMapTests.java @@ -41,7 +41,7 @@ public class RedisMapTests extends AbstractRedisMapTests { @Override RedisMap createMap() { - String redisName = getClass().getName(); + String redisName = getClass().getSimpleName(); return new DefaultRedisMap(redisName, template); } @@ -56,6 +56,7 @@ public class RedisMapTests extends AbstractRedisMapTests { } OxmSerializer serializer = new OxmSerializer(xstream, xstream); JacksonJsonRedisSerializer jsonSerializer = new JacksonJsonRedisSerializer(Person.class); + JacksonJsonRedisSerializer jsonStringSerializer = new JacksonJsonRedisSerializer(String.class); // create Jedis Factory ObjectFactory stringFactory = new StringObjectFactory(); @@ -77,8 +78,12 @@ public class RedisMapTests extends AbstractRedisMapTests { xstreamGenericTemplate.afterPropertiesSet(); // json - RedisTemplate jsonPersonTemplate = new RedisTemplate(jedisConnFactory); - jsonPersonTemplate.setValueSerializer(jsonSerializer); + RedisTemplate jsonPersonTemplate = new RedisTemplate(); + jsonPersonTemplate.setConnectionFactory(jedisConnFactory); + jsonPersonTemplate.setDefaultSerializer(jsonSerializer); + jsonPersonTemplate.setHashKeySerializer(jsonSerializer); + jsonPersonTemplate.setHashValueSerializer(jsonStringSerializer); + jsonPersonTemplate.afterPropertiesSet(); JredisConnectionFactory jredisConnFactory = new JredisConnectionFactory(); @@ -101,10 +106,13 @@ public class RedisMapTests extends AbstractRedisMapTests { xstreamPersonTemplateJR.setValueSerializer(serializer); // json JR - RedisTemplate jsonPersonTemplateJR = new RedisTemplate(jredisConnFactory); - jsonPersonTemplate.setValueSerializer(jsonSerializer); - - + RedisTemplate jsonPersonTemplateJR = new RedisTemplate(); + jsonPersonTemplateJR.setConnectionFactory(jredisConnFactory); + jsonPersonTemplateJR.setDefaultSerializer(jsonSerializer); + jsonPersonTemplateJR.setHashKeySerializer(jsonSerializer); + jsonPersonTemplateJR.setHashValueSerializer(jsonStringSerializer); + jsonPersonTemplateJR.afterPropertiesSet(); + return Arrays.asList(new Object[][] { { stringFactory, stringFactory, genericTemplate }, { personFactory, personFactory, genericTemplate }, { stringFactory, personFactory, genericTemplate }, { personFactory, stringFactory, genericTemplate }, @@ -114,9 +122,7 @@ public class RedisMapTests extends AbstractRedisMapTests { { stringFactory, personFactory, genericTemplateJR }, { personFactory, stringFactory, genericTemplateJR }, { personFactory, stringFactory, xGenericTemplateJR }, - { personFactory, personFactory, jsonPersonTemplate }, { personFactory, stringFactory, jsonPersonTemplate }, - { personFactory, personFactory, jsonPersonTemplateJR }, { personFactory, stringFactory, jsonPersonTemplateJR } }); } } \ No newline at end of file