1

https://docs.spring.io/spring-kafka/docs/current/api/org/springframework/kafka/core/KafkaTemplate.html#sendDefault(K,V)

Calling above method from kotlin with a nullable key results in compiler warnings. Shouldn't the key be nullable? It is legitimate to want to produce a message occasionally with no key.

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
Jacob Botuck
  • 411
  • 6
  • 22

1 Answers1

2

There are so many overloaded methods you can use one that accepts only topic and value if you don't want to pass the key

public ListenableFuture<SendResult<K,​V>> send​(java.lang.String topic, @Nullable V data)

As per the syntax from docs, only value can be null since it is annotated with @Nullable

public ListenableFuture<SendResult<K,​V>> send​(String topic, K key, @Nullable V data)
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • While this answer is correct, I don't see any problems with adding `@Nullable` to the key parameter. – Gary Russell Sep 24 '20 at 17:31
  • yeah, you guys already provided overloaded methods with variable args and i feel those are right ones to use as per requirement @GaryRussell – Ryuzaki L Sep 24 '20 at 17:47
  • Right now If I want to send a nullable key i need to write `key?.let { kafkaTemplate.sendDefault(it, value) }?: kafkaTemplate.sendDefault(value)`. If we add the annotation it becomes `kafkaTemplate.sendDefault(key, value)` – Jacob Botuck Sep 24 '20 at 19:19
  • 1
    i will suggest to use another overloaded method if `key` is null there is no point of sending null key @JacobBotuck – Ryuzaki L Sep 24 '20 at 19:20