Skip to content

Commit febd8ee

Browse files
garyrussellartembilan
authored andcommitted
GH-1784: Customizing JSON Serializer/Deserializer
Resolves #1784
1 parent 1f11735 commit febd8ee

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

spring-kafka-docs/src/main/asciidoc/kafka.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4002,6 +4002,8 @@ They have no effect if you have provided `Serializer` and `Deserializer` instanc
40024002
Starting with version 2.2, the type information headers (if added by the serializer) are removed by the deserializer.
40034003
You can revert to the previous behavior by setting the `removeTypeHeaders` property to `false`, either directly on the deserializer or with the configuration property described earlier.
40044004

4005+
See also <<tip-json>>.
4006+
40054007
[[serdes-mapping-types]]
40064008
====== Mapping Types
40074009

spring-kafka-docs/src/main/asciidoc/tips.adoc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,29 @@ public void sendToKafka(String in) {
171171
}
172172
----
173173
====
174+
175+
[[tip-json]]
176+
=== Customizing the JsonSerializer and JsonDeserializer
177+
178+
The serializer and deserializer support a number of cusomizations using properties, see <<json-serde>> for more information.
179+
The `kafka-clients` code, not Spring, instantiates these objects, unless you inject them directly into the consumer and producer factories.
180+
If you wish to configure the (de)serializer using properties, but wish to use, say, a custom `ObjectMapper`, simply create a subclass and pass the custom mapper into the `super` constructor. For example:
181+
182+
====
183+
[source, java]
184+
----
185+
public class CustomJsonSerializer extends JsonSerializer<Object> {
186+
187+
public CustomJsonSerializer() {
188+
super(customizedObjectMapper());
189+
}
190+
191+
private static ObjectMapper customizedObjectMapper() {
192+
ObjectMapper mapper = JacksonUtils.enhancedObjectMapper();
193+
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
194+
return mapper;
195+
}
196+
197+
}
198+
----
199+
====

0 commit comments

Comments
 (0)