Spring 无法对传入消息调用Kafka侦听器方法
我正在使用Spring Boot app将JSON数组转换为Kafka Producer中的toString(),从而发送JSON数组,但我在Consumer中遇到以下错误: org.springframework.kafka.listener.ListenerExecutionFailedException: 无法对传入消息调用侦听器方法 端点处理程序详细信息: 方法[public void com.springboot.service.KafkaReciever.receivedata(com.springboot.model.Student,java.lang.String) 抛出java.lang.Exception] Bean[com.springboot.service]。KafkaReciever@5bb3d42d]; 嵌套异常是 org.springframework.messaging.converter.MessageConversionException: 无法处理消息;嵌套异常是 org.springframework.messaging.converter.MessageConversionException: 无法从[java.lang.String]转换为 [com.springboot.model.Student]用于一般消息 [payload=[com.springboot.model。Student@5e40dc31, com.springboot.model。Student@235e68b8],headers={kafka_offset=45, kafka_receivedMessageKey=null,kafka_receivedPartitionId=0, 卡夫卡(u receivedTopic=myTopic kafkasender}], failedMessage=GenericMessage [payload=[com.springboot.model。Student@5e40dc31, com.springboot.model。Student@235e68b8],headers={kafka_offset=45, kafka_receivedMessageKey=null,kafka_receivedPartitionId=0, 卡夫卡(u receivedTopic=myTopic kafkasender}] 配置文件:Spring 无法对传入消息调用Kafka侦听器方法,spring,spring-boot,apache-kafka,kafka-consumer-api,Spring,Spring Boot,Apache Kafka,Kafka Consumer Api,我正在使用Spring Boot app将JSON数组转换为Kafka Producer中的toString(),从而发送JSON数组,但我在Consumer中遇到以下错误: org.springframework.kafka.listener.ListenerExecutionFailedException: 无法对传入消息调用侦听器方法 端点处理程序详细信息: 方法[public void com.springboot.service.KafkaReciever.receivedata(co
@Configuration
@EnableKafka
public class KafkaConsumerConfig {
@Value("${kafka.boot.server}")
private String kafkaServer;
@Value("${kafka.consumer.group.id}")
private String kafkaGroupId;
@Bean
public ConsumerFactory<String, String> consumerConfig() {
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put(ConsumerConfig.GROUP_ID_CONFIG, kafkaGroupId);
props.put("message.assembler.buffer.capacity", 33554432);
props.put("max.tracked.messages.per.partition", 24);
props.put("exception.on.message.dropped", true);
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put("segment.deserializer.class", DefaultSegmentDeserializer.class.getName());
return new DefaultKafkaConsumerFactory(props, null, new StringDeserializer());
}
@Bean
public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, String> listener = new ConcurrentKafkaListenerContainerFactory<>();
listener.setConsumerFactory(consumerConfig());
return listener;
}
}
发布json:
[{
"studentId": "Q45678123",
"firstName": "Anderson",
"lastName": "John",
"age": "12",
"address": {
"apartment": "apt 123",
"street": "street Info",
"state": "state",
"city": "city",
"postCode": "12345"
}
},
{
"studentId": "Q45678123",
"firstName": "abc",
"lastName": "xyz",
"age": "12",
"address": {
"apartment": "apt 123",
"street": "street Info",
"state": "state",
"city": "city",
"postCode": "12345"
}
}]
我得到了以下消费者产出:
[com.springboot.model.Student@5e40dc31, com.springboot.model.Student@235e68b8]
无法反序列化START\u数组之外的com.springboot.model.Student实例
如果使用json反序列化程序,则有一个列表,而不是一个学生
@Payload List<Student> student
关于您的其他输出,请参见您的消费者等待字符串(反序列化器),但它正在接收学生。您有权访问制作人的序列化程序吗?您需要这样的东西:我的模型数据是:public class Student实现可序列化的{private static final long serialVersionUID=1L;private String studentId;private String firstName;private String lastName;private String age;private Address Address;get/set}我将VALUE\u DESERIALIZER\u CLASS\u CONFIG更改为JsonDeserializer,然后出现以下错误:org.apache.kafka.common.errors.SerializationException:在偏移量47处反序列化分区myTopic-KafCaseder-0的键/值时出错,原因是:com.fasterxml.jackson.databind.JsonMappingException:无法反序列化com.springboot.model.Student的实例[Source:[B@304eaefb;第1行,第1列]ConsumerFactory请尝试使用您的有效负载对象类型,就像您的类名student然后是ConsumerFactory\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuprops.put(ConsumerConfig.VALUE\uDeserializer\uClass\uConfig,StringDeserializer.class);您可以使用json desrialzer,如果您需要正确的答案,请告诉我我会帮助您的you@harkesh谢谢..添加此错误,获得相同的错误..但在服务中更改为“@Payload List student”错误已解决,但数据未转换为json数组。谢谢。我已在producer中解析json数据,并已将其发送给consumer,之后我将获得正确的json数据在消费者中。我只是想检查一下,我这样做是否正确?只要没有例外,我会说没有错欢迎。如果他们已经解决了你的问题,请随意使用帖子旁边的复选标记接受答案
@Payload List<Student> student
@Payload String student