0

I have been trying to write a custom Serializer by extending the apache Serialzier class.

the serializer method contains to methods of importance to me

byte[] serialize(String topic, T data); 
default byte[] serialize(String topic, Headers headers, T data) {
       return serialize(topic, data);
   }

but whenever I write a custom implemetation for the serialzier it calls the overrided method

byte[] serialize(String topic, T data); 

Iam passing this class to kafka producer for serializer and hence I can't invoke these methods directly and the framework by default access the method.

But I want to invoke the default method instead of the overrided method. It is not possible to call the default method from other method since the headers are expected. Is there anyway to invoke default method instead of the overriden one while passing the serialzier for producer/consumer configuration?

pacman
  • 725
  • 1
  • 9
  • 28

1 Answers1

0

The fact that you've overwritten the method definition in the interface implementation means that's what's going to be called

It is not possible to call the default method from other method since the headers are expected

You should be able to pass null as the headers parameter

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • But headers are something I need to acess. Passing headers as null wouldn't let me access the header values right. – pacman Mar 20 '21 at 18:29
  • Based on your other questions about Avro, you don't need headers, no. If you do need access, then you also need to serialize them, and you wouldn't call `serialize(topic, data)` from the signature that includes the headers because you'd effectively be skipping them – OneCricketeer Mar 21 '21 at 15:04
  • Well the thing is I was thinking of schema registry like implementation. just saving the schema somewhere and adding some id in the header so that I can deserialize the data without schema. But right now Iam looking at sending the whole schema with the data. But this approach is an option. – pacman Mar 21 '21 at 16:07
  • Iam not trying to reinvent the wheel, instead Iam trying to understand how can I get this done without schema registry. – pacman Mar 21 '21 at 16:08
  • There's code in the link of this answer that does exactly that (not the ID part, because if you have the schema, there's no need for an ID) https://stackoverflow.com/a/62349172/2308683 – OneCricketeer Mar 22 '21 at 14:35
  • Iam getting the same output with the code above and the code I mentioned here https://stackoverflow.com/questions/66659843/java-io-ioexception-not-a-data-file-while-reading-avro-from-file?noredirect=1#comment117990387_66659843 – pacman Mar 23 '21 at 09:11
  • I have opened a question siting the problem I face here https://stackoverflow.com/questions/66763677/avro-seriallization-doesnt-send-avro-schema-along-with-data. Can you go through it and recommend what to do. – pacman Mar 23 '21 at 13:02