Skip to content

Commit cadd61d

Browse files
garyrussellartembilan
authored andcommitted
GH-739: Custom (De)Serializer Doc Improvement
Resolves #739 * Polishing - `<Foo, Bar>` instead of `<String, String>` * Fix typo
1 parent 43a3861 commit cadd61d

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

Diff for: spring-kafka/src/main/java/org/springframework/kafka/core/DefaultKafkaConsumerFactory.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.kafka.clients.consumer.KafkaConsumer;
2626
import org.apache.kafka.common.serialization.Deserializer;
2727

28+
import org.springframework.lang.Nullable;
2829
import org.springframework.util.StringUtils;
2930

3031
/**
@@ -48,13 +49,23 @@ public class DefaultKafkaConsumerFactory<K, V> implements ConsumerFactory<K, V>
4849

4950
private Deserializer<V> valueDeserializer;
5051

52+
/**
53+
* Construct a factory with the provided configuration.
54+
* @param configs the configuration.
55+
*/
5156
public DefaultKafkaConsumerFactory(Map<String, Object> configs) {
5257
this(configs, null, null);
5358
}
5459

60+
/**
61+
* Construct a factory with the provided configuration and deserializers.
62+
* @param configs the configuration.
63+
* @param keyDeserializer the key {@link Deserializer}.
64+
* @param valueDeserializer the value {@link Deserializer}.
65+
*/
5566
public DefaultKafkaConsumerFactory(Map<String, Object> configs,
56-
Deserializer<K> keyDeserializer,
57-
Deserializer<V> valueDeserializer) {
67+
@Nullable Deserializer<K> keyDeserializer,
68+
@Nullable Deserializer<V> valueDeserializer) {
5869
this.configs = new HashMap<>(configs);
5970
this.keyDeserializer = keyDeserializer;
6071
this.valueDeserializer = valueDeserializer;

Diff for: spring-kafka/src/main/java/org/springframework/kafka/core/DefaultKafkaProducerFactory.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
import org.springframework.beans.factory.DisposableBean;
4646
import org.springframework.context.Lifecycle;
47+
import org.springframework.lang.Nullable;
4748
import org.springframework.util.Assert;
4849

4950
/**
@@ -93,12 +94,23 @@ public class DefaultKafkaProducerFactory<K, V> implements ProducerFactory<K, V>,
9394

9495
private volatile boolean running;
9596

97+
/**
98+
* Construct a factory with the provided configuration.
99+
* @param configs the configuration.
100+
*/
96101
public DefaultKafkaProducerFactory(Map<String, Object> configs) {
97102
this(configs, null, null);
98103
}
99104

100-
public DefaultKafkaProducerFactory(Map<String, Object> configs, Serializer<K> keySerializer,
101-
Serializer<V> valueSerializer) {
105+
/**
106+
* Construct a factory with the provided configuration and {@link Serializer}s.
107+
* @param configs the configuration.
108+
* @param keySerializer the key {@link Serializer}.
109+
* @param valueSerializer the value {@link Serializer}.
110+
*/
111+
public DefaultKafkaProducerFactory(Map<String, Object> configs,
112+
@Nullable Serializer<K> keySerializer,
113+
@Nullable Serializer<V> valueSerializer) {
102114
this.configs = new HashMap<>(configs);
103115
this.keySerializer = keySerializer;
104116
this.valueSerializer = valueSerializer;

Diff for: src/reference/asciidoc/kafka.adoc

+28-4
Original file line numberDiff line numberDiff line change
@@ -1557,10 +1557,9 @@ props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
15571557
for more complex or particular cases, the `KafkaConsumer`, and therefore `KafkaProducer`, provides overloaded
15581558
constructors to accept `(De)Serializer` instances for `keys` and/or `values`, respectively.
15591559

1560-
To meet this API, the `DefaultKafkaProducerFactory` and `DefaultKafkaConsumerFactory` also provide properties to allow
1561-
to inject a custom `(De)Serializer` to target `Producer`/`Consumer`.
1560+
Using this API, the `DefaultKafkaProducerFactory` and `DefaultKafkaConsumerFactory` also provide properties (via constructors or setter methods) to inject custom `(De)Serializer` s to the target `Producer`/`Consumer`.
15621561

1563-
For this purpose, Spring for Apache Kafka also provides `JsonSerializer`/`JsonDeserializer` implementations based on the
1562+
Spring for Apache Kafka also provides `JsonSerializer`/`JsonDeserializer` implementations based on the
15641563
Jackson JSON object mapper.
15651564
The `JsonSerializer` is quite simple and just allows writing any Java object as a JSON `byte[]`, the `JsonDeserializer`
15661565
requires an additional `Class<?> targetType` argument to allow the deserialization of a consumed `byte[]` to the proper target
@@ -1583,6 +1582,31 @@ In addition, the serializer/deserializer can be configured using Kafka propertie
15831582
- `JsonDeserializer.VALUE_DEFAULT_TYPE`; fallback type for deserialization of values if no header information is present.
15841583
- `JsonDeserializer.TRUSTED_PACKAGES` (default `java.util`, `java.lang`); comma-delimited list of package patterns allowed for deserialization; `*` means deserialize all.
15851584

1585+
IMPORTANT: Only simple configuration can be performed with properties; for more advanced configuration (such as using a custom `ObjectMapper` in the serializer/deserializer), you should use the producer/consumer factory constructors that accept a pre-built serializer and deserializer. For example, with Spring Boot, to override the default factories:
1586+
1587+
[source, java]
1588+
----
1589+
@Bean
1590+
public ConsumerFactory<Foo, Bar> kafkaConsumerFactory(KafkaProperties properties,
1591+
JsonDeserializer customDeserializer) {
1592+
1593+
return new DefaultKafkaConsumerFactory<>(properties.buildConsumerProperties(),
1594+
customDeserializer, customDeserializer);
1595+
}
1596+
1597+
@Bean
1598+
public ProducererFactory<Foo, Bar> kafkaProducerFactory(KafkaProperties properties,
1599+
JsonSserializer customSerializer) {
1600+
1601+
return new DefaultKafkaConsumerFactory<>(properties.buildProducerProperties(),
1602+
customSerializer, customSerializer);
1603+
}
1604+
----
1605+
1606+
Setters are also provided, as an alternative to using these constructors.
1607+
1608+
===== Spring Messaging Message Conversion
1609+
15861610
Although the `Serializer`/`Deserializer` API is quite simple and flexible from the low-level Kafka `Consumer` and
15871611
`Producer` perspective, you might need more flexibility at the Spring Messaging level, either when using `@KafkaListener` or <<si-kafka,Spring Integration>>.
15881612
To easily convert to/from `org.springframework.messaging.Message`, Spring for Apache Kafka provides a `MessageConverter`
@@ -1617,7 +1641,7 @@ With a class-level `@KafkaListener`, the payload type is used to select which `@
16171641
====
16181642

16191643
NOTE: When using the `StringJsonMessageConverter`, you should use a `StringDeserializer` in the kafka consumer configuration and `StringSerializer` in the kafka producer configuration, when using Spring Integration or the `KafkaTemplate.send(Message<?> message)` method.
1620-
When using the `BytesJsonMessageConverter`, you should use a `BytesDeserializer` in the kafka consumer configuration and `BytesSerializer` in the kafka producer configuration, when using Spring Integration or the `KafkaTemplate.send(Message<?> message)` method.
1644+
When using the `BytesJsonMessageConverter`, you should use a `BytesDeserializer` in the kafka consumer configuration and `BytesSerializer` in the kafka producer configuration, when using Spring Integration or the `KafkaTemplate.send(Message<?> message)` method (see <<kafka-template>>).
16211645
Generally, the `BytesJsonMessageConverter` is more efficient because it avoids a `String` to/from `byte[]` conversion.
16221646

16231647
[[error-handling-deserializer]]

0 commit comments

Comments
 (0)