Skip to content

Commit 8e5c5a7

Browse files
garyrussellartembilan
authored andcommitted
GH-1870: Fix Doc for Programmatic JSON Config
Resolves #1870 Also remove references to `KafkaProperties`, which the Boot team doesn't consider a public API.
1 parent cd661dc commit 8e5c5a7

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

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

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3675,24 +3675,24 @@ public class Application {
36753675
}
36763676
36773677
@Bean
3678-
public ConsumerFactory<?, ?> kafkaConsumerFactory(KafkaProperties properties, SomeBean someBean) {
3679-
Map<String, Object> consumerProperties = properties.buildConsumerProperties();
3678+
public ConsumerFactory<?, ?> kafkaConsumerFactory(SomeBean someBean) {
3679+
Map<String, Object> consumerProperties = new HashMap<>();
3680+
// consumerProperties.put(..., ...)
3681+
// ...
36803682
consumerProperties.put(ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG, MyConsumerInterceptor.class.getName());
36813683
consumerProperties.put("some.bean", someBean);
36823684
return new DefaultKafkaConsumerFactory<>(consumerProperties);
36833685
}
36843686
36853687
@Bean
3686-
public ProducerFactory<?, ?> kafkaProducerFactory(KafkaProperties properties, SomeBean someBean) {
3688+
public ProducerFactory<?, ?> kafkaProducerFactory(SomeBean someBean) {
3689+
Map<String, Object> producerProperties = new HashMap<>();
3690+
// producerProperties.put(..., ...)
3691+
// ...
36873692
Map<String, Object> producerProperties = properties.buildProducerProperties();
36883693
producerProperties.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, MyProducerInterceptor.class.getName());
36893694
producerProperties.put("some.bean", someBean);
36903695
DefaultKafkaProducerFactory<?, ?> factory = new DefaultKafkaProducerFactory<>(producerProperties);
3691-
String transactionIdPrefix = properties.getProducer()
3692-
.getTransactionIdPrefix();
3693-
if (transactionIdPrefix != null) {
3694-
factory.setTransactionIdPrefix(transactionIdPrefix);
3695-
}
36963696
return factory;
36973697
}
36983698
@@ -4075,19 +4075,19 @@ The following Spring Boot example overrides the default factories:
40754075
[source, java]
40764076
----
40774077
@Bean
4078-
public ConsumerFactory<Foo, Bar> kafkaConsumerFactory(KafkaProperties properties,
4079-
JsonDeserializer customDeserializer) {
4080-
4081-
return new DefaultKafkaConsumerFactory<>(properties.buildConsumerProperties(),
4082-
customDeserializer, customDeserializer);
4078+
public ConsumerFactory<String, Thing> kafkaConsumerFactory(JsonDeserializer customValueDeserializer) {
4079+
Map<String, Object> properties = new HashMap<>();
4080+
// properties.put(..., ...)
4081+
// ...
4082+
return new DefaultKafkaConsumerFactory<>(properties,
4083+
new StringDeserializer(), customValueDeserializer);
40834084
}
40844085
40854086
@Bean
4086-
public ProducerFactory<Foo, Bar> kafkaProducerFactory(KafkaProperties properties,
4087-
JsonSerializer customSerializer) {
4087+
public ProducerFactory<String, Thing> kafkaProducerFactory(JsonSerializer customValueSerializer) {
40884088
40894089
return new DefaultKafkaProducerFactory<>(properties.buildProducerProperties(),
4090-
customSerializer, customSerializer);
4090+
new StringSerializer(), customValueSerializer);
40914091
}
40924092
----
40934093
=====
@@ -4163,31 +4163,35 @@ public static JavaType thing1Thing2JavaTypeForTopic(String topic, byte[] data, H
41634163

41644164
When constructing the serializer/deserializer programmatically for use in the producer/consumer factory, since version 2.3, you can use the fluent API, which simplifies configuration.
41654165

4166-
The following example assumes you are using Spring Boot:
4167-
41684166
====
41694167
[source, java]
41704168
----
41714169
@Bean
4172-
public DefaultKafkaProducerFactory pf(KafkaProperties properties) {
4173-
Map<String, Object> props = properties.buildProducerProperties();
4174-
DefaultKafkaProducerFactory pf = new DefaultKafkaProducerFactory(props,
4175-
new JsonSerializer<>(MyKeyType.class)
4170+
public ProducerFactory<MyKeyType, MyValueType> pf() {
4171+
Map<String, Object> props = new HashMap<>();
4172+
// props.put(..., ...)
4173+
// ...
4174+
DefaultKafkaProducerFactory<MyKeyType, MyValueType> pf = new DefaultKafkaProducerFactory<>(props,
4175+
new JsonSerializer<MyKeyType>()
41764176
.forKeys()
41774177
.noTypeInfo(),
4178-
new JsonSerializer<>(MyValueType.class)
4178+
new JsonSerializer<MyValueType>()
41794179
.noTypeInfo());
4180+
return pf;
41804181
}
41814182
41824183
@Bean
4183-
public DefaultKafkaConsumerFactory pf(KafkaProperties properties) {
4184-
Map<String, Object> props = properties.buildConsumerProperties();
4185-
DefaultKafkaConsumerFactory pf = new DefaultKafkaConsumerFactory(props,
4184+
public ConsumerFactory<MyKeyType, MyValueType> cf() {
4185+
Map<String, Object> props = new HashMap<>();
4186+
// props.put(..., ...)
4187+
// ...
4188+
DefaultKafkaConsumerFactory<MyKeyType, MyValueType> cf = new DefaultKafkaConsumerFactory<>(props,
41864189
new JsonDeserializer<>(MyKeyType.class)
41874190
.forKeys()
41884191
.ignoreTypeHeaders(),
41894192
new JsonDeserializer<>(MyValueType.class)
41904193
.ignoreTypeHeaders());
4194+
return cf;
41914195
}
41924196
----
41934197
====

0 commit comments

Comments
 (0)