You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add support for Jackson 3-based HashMapper and RedisSerializer.
We now support Jackson 3 through a separate Jackson3HashMapper, Jackson3JsonRedisSerializer, and GenericJackson3JsonRedisSerializer.
Jackson 3 uses different defaults than Jackson 2 did therefore, we also provide configuration variants that reflect Jackson 2 behavior for smoother upgrade paths.
Original pull request: #3168Closes#3154
Copy file name to clipboardExpand all lines: src/main/antora/modules/ROOT/pages/redis/hash-mappers.adoc
+85-2Lines changed: 85 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
[[redis.hashmappers.root]]
2
2
= Hash Mapping
3
3
4
-
Data can be stored by using various data structures within Redis. javadoc:org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer[] can convert objects in https://en.wikipedia.org/wiki/JSON[JSON] format. Ideally, JSON can be stored as a value by using plain keys. You can achieve a more sophisticated mapping of structured objects by using Redis hashes. Spring Data Redis offers various strategies for mapping data to hashes (depending on the use case):
4
+
Data can be stored by using various data structures within Redis. javadoc:org.springframework.data.redis.serializer.Jackson3JsonRedisSerializer[] can convert objects in https://en.wikipedia.org/wiki/JSON[JSON] format. Ideally, JSON can be stored as a value by using plain keys. You can achieve a more sophisticated mapping of structured objects by using Redis hashes. Spring Data Redis offers various strategies for mapping data to hashes (depending on the use case):
5
5
6
6
* Direct mapping, by using javadoc:org.springframework.data.redis.core.HashOperations[] and a xref:redis.adoc#redis:serializer[serializer]
7
7
* Using xref:repositories.adoc[Redis Repositories]
@@ -16,7 +16,8 @@ Multiple implementations are available:
16
16
17
17
* javadoc:org.springframework.data.redis.hash.BeanUtilsHashMapper[] using Spring's {spring-framework-javadoc}/org/springframework/beans/BeanUtils.html[BeanUtils].
18
18
* javadoc:org.springframework.data.redis.hash.ObjectHashMapper[] using xref:redis/redis-repositories/mapping.adoc[Object-to-Hash Mapping].
19
-
* <<redis.hashmappers.jackson2,`Jackson2HashMapper`>> using https://github.com/FasterXML/jackson[FasterXML Jackson].
19
+
* <<redis.hashmappers.jackson3,`Jackson3HashMapper`>> using https://github.com/FasterXML/jackson[FasterXML Jackson 3].
20
+
* <<redis.hashmappers.jackson2,`Jackson2HashMapper`>> (deprecated) using https://github.com/FasterXML/jackson[FasterXML Jackson 2].
20
21
21
22
The following example shows one way to implement hash mapping:
22
23
@@ -50,9 +51,91 @@ public class HashMapping {
50
51
}
51
52
----
52
53
54
+
[[redis.hashmappers.jackson3]]
55
+
=== Jackson3HashMapper
56
+
57
+
javadoc:org.springframework.data.redis.hash.Jackson3HashMapper[] provides Redis Hash mapping for domain objects by using https://github.com/FasterXML/jackson[FasterXML Jackson 3].
58
+
`Jackson3HashMapper` can map top-level properties as Hash field names and, optionally, flatten the structure.
59
+
Simple types map to simple values. Complex types (nested objects, collections, maps, and so on) are represented as nested JSON.
60
+
61
+
Flattening creates individual hash entries for all nested properties and resolves complex types into simple types, as far as possible.
62
+
63
+
Consider the following class and the data structure it contains:
64
+
65
+
[source,java]
66
+
----
67
+
public class Person {
68
+
String firstname;
69
+
String lastname;
70
+
Address address;
71
+
Date date;
72
+
LocalDateTime localDateTime;
73
+
}
74
+
75
+
public class Address {
76
+
String city;
77
+
String country;
78
+
}
79
+
----
80
+
81
+
The following table shows how the data in the preceding class would appear in normal mapping:
82
+
83
+
.Normal Mapping
84
+
[width="80%",cols="<1,<2",options="header"]
85
+
|====
86
+
|Hash Field
87
+
|Value
88
+
89
+
|firstname
90
+
|`Jon`
91
+
92
+
|lastname
93
+
|`Snow`
94
+
95
+
|address
96
+
|`{ "city" : "Castle Black", "country" : "The North" }`
97
+
98
+
|date
99
+
|1561543964015
100
+
101
+
|localDateTime
102
+
|`2018-01-02T12:13:14`
103
+
|====
104
+
105
+
The following table shows how the data in the preceding class would appear in flat mapping:
106
+
107
+
.Flat Mapping
108
+
[width="80%",cols="<1,<2",options="header"]
109
+
|====
110
+
|Hash Field
111
+
|Value
112
+
113
+
|firstname
114
+
|`Jon`
115
+
116
+
|lastname
117
+
|`Snow`
118
+
119
+
|address.city
120
+
|`Castle Black`
121
+
122
+
|address.country
123
+
|`The North`
124
+
125
+
|date
126
+
|1561543964015
127
+
128
+
|localDateTime
129
+
|`2018-01-02T12:13:14`
130
+
|====
131
+
132
+
NOTE: Flattening requires all property names to not interfere with the JSON path. Using dots or brackets in map keys or as property names is not supported when you use flattening. The resulting hash cannot be mapped back into an Object.
133
+
53
134
[[redis.hashmappers.jackson2]]
54
135
=== Jackson2HashMapper
55
136
137
+
WARNING: Jackson 2 based implementations have been deprecated and are subject to removal in a subsequent release.
138
+
56
139
javadoc:org.springframework.data.redis.hash.Jackson2HashMapper[] provides Redis Hash mapping for domain objects by using https://github.com/FasterXML/jackson[FasterXML Jackson].
57
140
`Jackson2HashMapper` can map top-level properties as Hash field names and, optionally, flatten the structure.
58
141
Simple types map to simple values. Complex types (nested objects, collections, maps, and so on) are represented as nested JSON.
Copy file name to clipboardExpand all lines: src/main/antora/modules/ROOT/pages/redis/template.adoc
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -365,7 +365,7 @@ Multiple implementations are available (including two that have been already men
365
365
* javadoc:org.springframework.data.redis.serializer.JdkSerializationRedisSerializer[], which is used by default for javadoc:org.springframework.data.redis.cache.RedisCache[] and javadoc:org.springframework.data.redis.core.RedisTemplate[].
366
366
* the `StringRedisSerializer`.
367
367
368
-
However, one can use `OxmSerializer` for Object/XML mapping through Spring {spring-framework-docs}/data-access.html#oxm[OXM] support or javadoc:org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer[] or javadoc:org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer[] for storing data in https://en.wikipedia.org/wiki/JSON[JSON] format.
368
+
However, one can use `OxmSerializer` for Object/XML mapping through Spring {spring-framework-docs}/data-access.html#oxm[OXM] support or javadoc:org.springframework.data.redis.serializer.Jackson3JsonRedisSerializer[] or javadoc:org.springframework.data.redis.serializer.GenericJackson3JsonRedisSerializer[] for storing data in https://en.wikipedia.org/wiki/JSON[JSON] format.
369
369
370
370
Do note that the storage format is not limited only to values.
371
371
It can be used for keys, values, or hashes without any restrictions.
0 commit comments