将自定义 Java 对象发送到 Kafka 主题
我有我的自定义Java对象,并希望在内置序列化中利用JVM将其发送到Kafka主题,但序列化失败并出现以下错误
org.apache.kafka.common.errors.SerializationException:无法将类 com.spring.kafka.Payload 的值转换为在 value.serializer 中指定的类 org.apache.kafka.common.serialization.ByteArraySerializer
有效载荷.java
public class Payload implements Serializable {
private static final long serialVersionUID = 123L;
private String name="vinod";
private int anInt = 5;
private Double aDouble = new Double("5.0");
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAnInt() {
return anInt;
}
public void setAnInt(int anInt) {
this.anInt = anInt;
}
public Double getaDouble() {
return aDouble;
}
public void setaDouble(Double aDouble) {
this.aDouble = aDouble;
}
}
在我创建生产者期间,我设置了以下属性
<entry key="key.serializer"
value="org.apache.kafka.common.serialization.ByteArraySerializer" />
<entry key="value.serializer"
value="org.apache.kafka.common.serialization.ByteArraySerializer" />
我的发送调用如下所示
kafkaProducer.send(new ProducerRecord<String, Payload>("test", new Payload()));
通过生产者将自定义java对象发送到kafka主题的正确方法是什么?