遇到了一个有趣的案例:有一些“空间”(Mq-queue,Kafka-topic,simple folder,诸如此类)。在这个空间中,以序列化的形式编写Java对象。有一个监听程序监听此空间。问题是:作为这个侦听器,我如何才能从这个序列化对象中获取一些数据,而不是在侦听器中实现这个类。
我真的不想看到这个类的所有逻辑。查看对象字段就足够了(例如,map: someObjectField=>someFieldValue)。
现在,当我尝试反序列化这个对象时,我捕捉到了“"classNotFoundException”。有没有可能避免这个问题,而不用重写所有默认的Java反序列化逻辑?(也许有些工具已经存在了?)
发布于 2018-05-17 22:48:01
我认为您没有指定反序列化程序。实际上,在创建消费者时,您需要在消费者的属性中提供反序列化程序类,以及引导服务器和其他属性。例如:
public class KafkaConsumerExample {
...
private static Consumer<Long, String> createConsumer() {
final Properties props = new Properties();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
BOOTSTRAP_SERVERS);
props.put(ConsumerConfig.GROUP_ID_CONFIG,
"KafkaExampleConsumer");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
LongDeserializer.class.getName());
//depends on your key serialiser if it’s long then you should use long or else what ever data type you use
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
StringDeserializer.class.getName());
// Create the consumer using props.
final Consumer<Long, String> consumer =
new KafkaConsumer<>(props);
// Subscribe to the topic.
consumer.subscribe(Collections.singletonList(TOPIC));
return consumer;
}
...
}如果您正在使用某个自定义序列化程序,则必须创建自定义反序列化程序。
https://stackoverflow.com/questions/50391576
复制相似问题