gpt4 book ai didi

java - 反序列化 avro 对象时出现 spring MessageConversionException

转载 作者:行者123 更新时间:2023-12-02 00:05:17 26 4
gpt4 key购买 nike

我正在发送一个 avro 对象 User 并在监听器中接收它。这里是配置

@Bean
public ProducerFactory<String, User> userProducerFactory() {
Map<String, Object> config = new HashMap<>();

config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, AvroSerializer.class);

return new DefaultKafkaProducerFactory<>(config);
}

@Bean
public KafkaTemplate<String, User> userKafkaTemplate() {
return new KafkaTemplate<>(userProducerFactory());
}

@Bean
public Map<String, Object> consumerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, AvroDeserializer.class);
props.put(ConsumerConfig.GROUP_ID_CONFIG, "avro");

return props;
}

@Bean
public ConsumerFactory<String, User> consumerFactory() {
return new DefaultKafkaConsumerFactory<>(consumerConfigs(), new StringDeserializer(),
new AvroDeserializer<>(User.class));
}

@Bean
public ConcurrentKafkaListenerContainerFactory<String, User> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, User> factory
= new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());

return factory;
}

我正在像这样发送给用户

@Autowired
KafkaTemplate<String, User> userProducer;
public static void main(String[] args) {
SpringApplication.run(EvaluatorApplication.class, args);
}

@Override
public void run(String... args) throws Exception {

User user = User.newBuilder()
.setFavoriteColor("red")
.setFavoriteNumber(123)
.setName("Shantanu")
.build();

userProducer.send("avro.t", user);
}

这是 Kafka 监听器

@KafkaListener(topics = "avro.t", containerFactory = "kafkaListenerContainerFactory", groupId = "transaction-request-upstream-consumer")
public void listenUpstream(User user)
{
try {
LOGGER.info("Received {}", user);
messageProcessor.process(null, null);
//ack.acknowledge();
} catch (Exception ex) {
LOGGER.error("{}", ex);
}
}

在 AvroDeserializer 类中,消息被反序列化,但我得到的是这个异常。我无法理解它的正面或反面。花了很多时间,完全看不懂。请帮忙!!!

[2020-03-26 00:59:16.358] :: [ERROR] :: o.s.k.listener.LoggingErrorHandler.error.254 :: Error while processing: ConsumerRecord(topic = avro.t, partition = 0, leaderEpoch = 0, offset = 4, CreateTime = 1585164556293, serialized key size = -1, serialized value size = 17, headers = RecordHeaders(headers = [], isReadOnly = false), key = null, value = {"name": "Shantanu", "favorite_number": 123, "favorite_color": "red"})
org.springframework.kafka.listener.ListenerExecutionFailedException: Listener method could not be invoked with the incoming message
Endpoint handler details:
Method [public void com.ibm.dip.evaluator.listeners.EventListener.listenUpstream(com.ibm.dip.evaluator.models.User)]
Bean [com.ibm.dip.evaluator.listeners.EventListener@356dc38d]; nested exception is org.springframework.messaging.converter.MessageConversionException: Cannot handle message; nested exception is org.springframework.messaging.converter.MessageConversionException: Cannot convert from [com.ibm.dip.evaluator.models.User] to [com.ibm.dip.evaluator.models.User] for GenericMessage [payload={"name": "Shantanu", "favorite_number": 123, "favorite_color": "red"}, headers={kafka_offset=4, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@50daf984, kafka_timestampType=CREATE_TIME, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_receivedTopic=avro.t, kafka_receivedTimestamp=1585164556293, kafka_groupId=transaction-request-upstream-consumer}], failedMessage=GenericMessage [payload={"name": "Shantanu", "favorite_number": 123, "favorite_color": "red"}, headers={kafka_offset=4, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@50daf984, kafka_timestampType=CREATE_TIME, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_receivedTopic=avro.t, kafka_receivedTimestamp=1585164556293, kafka_groupId=transaction-request-upstream-consumer}]; nested exception is org.springframework.messaging.converter.MessageConversionException: Cannot handle message; nested exception is org.springframework.messaging.converter.MessageConversionException: Cannot convert from [com.ibm.dip.evaluator.models.User] to [com.ibm.dip.evaluator.models.User] for GenericMessage [payload={"name": "Shantanu", "favorite_number": 123, "favorite_color": "red"}, headers={kafka_offset=4, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@50daf984, kafka_timestampType=CREATE_TIME, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_receivedTopic=avro.t, kafka_receivedTimestamp=1585164556293, kafka_groupId=transaction-request-upstream-consumer}], failedMessage=GenericMessage [payload={"name": "Shantanu", "favorite_number": 123, "favorite_color": "red"}, headers={kafka_offset=4, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@50daf984, kafka_timestampType=CREATE_TIME, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_receivedTopic=avro.t, kafka_receivedTimestamp=1585164556293, kafka_groupId=transaction-request-upstream-consumer}]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.decorateException(KafkaMessageListenerContainer.java:1774)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeErrorHandler(KafkaMessageListenerContainer.java:1766)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeRecordListener(KafkaMessageListenerContainer.java:1679)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeWithRecords(KafkaMessageListenerContainer.java:1605)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeRecordListener(KafkaMessageListenerContainer.java:1510)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeListener(KafkaMessageListenerContainer.java:1257)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.pollAndInvoke(KafkaMessageListenerContainer.java:1007)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.run(KafkaMessageListenerContainer.java:927)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.messaging.converter.MessageConversionException: Cannot handle message; nested exception is org.springframework.messaging.converter.MessageConversionException: Cannot convert from [com.ibm.dip.evaluator.models.User] to [com.ibm.dip.evaluator.models.User] for GenericMessage [payload={"name": "Shantanu", "favorite_number": 123, "favorite_color": "red"}, headers={kafka_offset=4, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@50daf984, kafka_timestampType=CREATE_TIME, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_receivedTopic=avro.t, kafka_receivedTimestamp=1585164556293, kafka_groupId=transaction-request-upstream-consumer}], failedMessage=GenericMessage [payload={"name": "Shantanu", "favorite_number": 123, "favorite_color": "red"}, headers={kafka_offset=4, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@50daf984, kafka_timestampType=CREATE_TIME, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_receivedTopic=avro.t, kafka_receivedTimestamp=1585164556293, kafka_groupId=transaction-request-upstream-consumer}]
at org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:336)
at org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter.onMessage(RecordMessagingMessageListenerAdapter.java:86)
at org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter.onMessage(RecordMessagingMessageListenerAdapter.java:51)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeOnMessage(KafkaMessageListenerContainer.java:1728)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeOnMessage(KafkaMessageListenerContainer.java:1711)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeRecordListener(KafkaMessageListenerContainer.java:1666)
... 8 common frames omitted
Caused by: org.springframework.messaging.converter.MessageConversionException: Cannot convert from [com.ibm.dip.evaluator.models.User] to [com.ibm.dip.evaluator.models.User] for GenericMessage [payload={"name": "Shantanu", "favorite_number": 123, "favorite_color": "red"}, headers={kafka_offset=4, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@50daf984, kafka_timestampType=CREATE_TIME, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_receivedTopic=avro.t, kafka_receivedTimestamp=1585164556293, kafka_groupId=transaction-request-upstream-consumer}]
at org.springframework.messaging.handler.annotation.support.PayloadMethodArgumentResolver.resolveArgument(PayloadMethodArgumentResolver.java:145)
at org.springframework.kafka.annotation.KafkaListenerAnnotationBeanPostProcessor$KafkaNullAwarePayloadArgumentResolver.resolveArgument(KafkaListenerAnnotationBeanPostProcessor.java:906)
at org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:117)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:148)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:116)
at org.springframework.kafka.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:48)
at org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:326)
... 13 common frames omitted

最佳答案

这很奇怪。在我尝试的多种组合中,我开始知道 spring boot 版本 2.2.5 + spring-boot dev-tools 依赖导致了这个问题。无法解释原因。但是当我切换到 spring-boot 2.1.5 并删除开发工具时,一切都开始正常工作。

关于java - 反序列化 avro 对象时出现 spring MessageConversionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60856177/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com