title: Custom conversion of Apache Kafka header using Spring Kafka
body: <p>I am using Spring Boot 2.3.0 with Spring Kafka 2.5.0 in in my KafkaListener I am trying to map  to map of the MessageHeaders to a custom class. The code below works, but gives me the header in byte[] that I would then have to convert to the class inside the listener (and repeat that for every listener), which I would like to avoid.</p>
  <pre><code>    @Slf4j
      @Component
      @KafkaListener(topics = {&quot;${spring.kafka.topics.simple}&quot;}, groupId = &quot;consumerGroup&quot;, 
      containerFactory = &quot;kafkaListenerContainerFactory&quot;)
      public class RequestConsumer {
  
         @KafkaHandler
         public void listen(@Payload CustomerDetails customerDetails, @Header(&quot;sec&quot;) byte[] 
         principle, @Headers MessageHeaders messageHeaders) {
             log.info(&quot;Received a CustomerDetails&quot;);
         }
  
         @KafkaHandler(isDefault = true)
         public void listen(@Payload(required = false) GenericRecord object, @Headers 
         MessageHeaders messageHeaders) {
             log.info(&quot;Received an unexpected object&quot;);
         }
      }
  </code></pre>
  <p>THe moment i change the code to this:</p>
  <pre><code>    @KafkaHandler
      public void listen(@Payload CustomerDetails customerDetails, @Header(&quot;sec&quot;) PreAuthenticatedAuthenticationToken principle, @Headers MessageHeaders messageHeaders) {
          log.info(&quot;Received a CustomerDetails&quot;);
      }
  </code></pre>
  <p>The listener will break with this error:</p>
  <p>org.springframework.kafka.listener.ListenerExecutionFailedException: Listener method 'public void com.example.demo.consumer.RequestConsumer.listen(com.example.schemas.CustomerDetails,org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken,org.springframework.messaging.MessageHeaders)' threw exception; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [byte[]] to type [@org.springframework.messaging.handler.annotation.Header org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken]; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [byte[]] to type [@org.springframework.messaging.handler.annotation.Header org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken]</p>
  <p>I've looked over the <a href="https://docs.spring.io/spring-kafka/reference/html/#headers" rel="nofollow noreferrer">https://docs.spring.io/spring-kafka/reference/html/#headers</a> documentation which talks about header mapping from Headers to MessageHeaders and vise versa, but has no examples of converting those headers. The error seems to suggest that I can register a convertor somehow, but have not found out how despite many Google searches and trying to step through the code of Spring Kafka.</p>
  <p>Help on this would be greatly appreciated.</p>
  <p>Cheers,
  Oskar</p>