1

Background

Building following class

public class DerivedClass<T> extends BaseClass<T> {
    
      @Override
      public void invoke(T value, Context context) throws Exception {
                //Send data to Kafka
                // Need to covert to byte[]
      }
}

Problem

Not sure if this makes sense, but need to convert this value to byte array e.g. In case of String, it is possible using getBytes() method. How to achieve this ?

Albatross
  • 669
  • 7
  • 24
  • Does this answer your question? [Java Serializable Object to Byte Array](https://stackoverflow.com/questions/2836646/java-serializable-object-to-byte-array) – azurefrog May 19 '21 at 22:50
  • Also, here's [a basic tutorial](https://www.tutorialspoint.com/How-to-convert-an-object-to-byte-array-in-java) for the same thing. – azurefrog May 19 '21 at 22:50

1 Answers1

2

getBytes() is a method specific to String so you need a more general way to convert objects, not just Strings, to a Byte array. This post seems to explain how to do that: https://stackoverflow.com/a/2836659/6630965.

I believe this can only be done on objects that implement Serializable though, so you'll probably want to specify that in your generic type, like this:

DerivedClass<T extends Serializable>
Wes1324
  • 1,077
  • 8
  • 13