Apache kafka JsonSerializer工作不正常kafka
我正在使用kafka我有一个通知类,我正在使用SpringKafka序列化它Apache kafka JsonSerializer工作不正常kafka,apache-kafka,spring-kafka,Apache Kafka,Spring Kafka,我正在使用kafka我有一个通知类,我正在使用SpringKafka序列化它 package com.code2hack.notification; public class Notification { private Object message; private NotificationType type; public static Notification create(NotificationType type, Object message){
package com.code2hack.notification;
public class Notification {
private Object message;
private NotificationType type;
public static Notification create(NotificationType type, Object message){
return new Notification(message,type);
}
public Notification(){
}
public Notification(Object message, NotificationType type){
this.message = message;
this.type = type;
}
@Override
public String toString() {
return "Notification{" +
"message=" + message +
", type=" + type +
'}';
}
public <T> T getMessage(Class<T> type){
return (T)this.message;
}
public NotificationType getType(){
return this.type;
}
public void setType(NotificationType type){
this.type = type;
}
public void setMessage(Object message){
this.message = message;
}
}
当我尝试使用来自消费者的通知时,我的消息部分在通知中丢失,我可以接收类型
我甚至试过kafka控制台消费者,它只打印我的通知消息中的类型字段,这里也没有
我不知道我错过了什么
我的消费者配置是
package com.code2hack.booking;
import com.code2hack.notification.Notification;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.support.serializer.JsonDeserializer;
import java.util.HashMap;
import java.util.Map;
@Configuration
@EnableKafka
public class KafkaConfiguration {
@Value("${spring.kafka.consumer.bootstrap-servers}")
private String address;
@Bean
public ConsumerFactory<String, Notification> consumerFactory() {
Map<String, Object> props = new HashMap<>();
props.put(
ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
address);
props.put(
ConsumerConfig.GROUP_ID_CONFIG,
"booking");
JsonDeserializer<Notification> ds = new JsonDeserializer<>();
ds.addTrustedPackages("*");
return new DefaultKafkaConsumerFactory<>(props,
new StringDeserializer(),
ds);
}
@Bean
public ConcurrentKafkaListenerContainerFactory<String, Notification>
kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, Notification> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
return factory;
}
}
请帮帮我
注意:实际上问题在于卡夫卡中的JsonSerializer。我尝试了下面的代码,但它没有正确地序列化对象
public static void main(String[] args) {
//SpringApplication.run(BookingServiceApplication.class, args);
Notification notification = Notification.create(NotificationType.NEW_SCHEDULED,"Hellow how are you");
byte[] serialize = new JsonSerializer<Notification>().serialize("sql-insert", notification);
System.out.println(new String(serialize));
}
有没有办法修复它。除非您创建自定义序列化程序,否则Jackson只能使用JavaBean语义;没有用于
消息的getter
;您需要为message属性添加一个简单的getter
@KafkaListener(topics = "sql-insert",groupId = "booking")
public void onNotification(@Payload Notification notification){
handleNotification(notification);
}
public static void main(String[] args) {
//SpringApplication.run(BookingServiceApplication.class, args);
Notification notification = Notification.create(NotificationType.NEW_SCHEDULED,"Hellow how are you");
byte[] serialize = new JsonSerializer<Notification>().serialize("sql-insert", notification);
System.out.println(new String(serialize));
}
{"type":"NEW_SCHEDULED"}