The Jackson2JsonRedisSerializer
不包括到实际哈希结构的映射信息。
生成的 Redis HASH 结果类似于:
127.0.0.1:6379> hgetall job:1
1) "id"
2) "\"1\""
3) "createTime"
4) "1455778716799"
5) "submitterName"
6) "\"Jon Snow\""
7) "jobDef"
8) "{\"def\":\"nightwatch\"}"
The ObjectMapper
产生一个LinkedHashMap
为了JobDefinition
由于类型未知而无法反序列化的条目。
使用GenericJackson2JsonRedisSerializer
包含类型信息,因此生成的 Redis HASH 如下所示:
127.0.0.1:6379> hgetall job:1
1) "id"
2) "\"1\""
...
7) "jobDef"
8) "{\"@class\":\"java.util.LinkedHashMap\",\"def\":\"nightwatch\"}"
这允许正确反序列化值。
另一种方法是NOT使用特定的HashValueSerializer
但改为使用DecoratingStringHashMapper
随着StringRedisTemplate
.
DecoratingStringHashMapper mapper = new DecoratingStringHashMapper<Job>(
new JacksonHashMapper<Job>(Job.class));
template.opsForHash().putAll("job:" + job.id, mapper.toHash(job));
Map jobMap = template.opsForHash().entries("job:" + job.id);
The DecoratingStringHashMapper
将产生如下 Redis 哈希:
127.0.0.1:6379> hgetall job:1
1) "id"
2) "1"
3) "createTime"
4) "1455780810643"
5) "submitterName"
6) "Jon Snow"
7) "jobDef"
8) "{def=nightwatch}"
不幸的是,这儿没有Jackson2HashMapper
。请投票给DATAREDIS-423 https://jira.spring.io/browse/DATAREDIS-423并帮助我们确定优先顺序。