无法使用传入消息调用 Kafka Listener 方法
Kafka Listener method could not be invoked with the incoming message
我正在使用 Spring Boot 应用程序通过将其转换为 Kafka Producer 中的 toString() 来发送一个 JSON 数组,但我在 Consumer 中收到以下错误:
org.springframework.kafka.listener.ListenerExecutionFailedException:
Listener method could not be invoked with the incoming message
Endpoint handler details:
Method [public void com.springboot.service.KafkaReciever.recieveData(com.springboot.model.Student,java.lang.String)
throws java.lang.Exception]
Bean [com.springboot.service.KafkaReciever@5bb3d42d]; nested exception is
org.springframework.messaging.converter.MessageConversionException:
Cannot handle message; nested exception is
org.springframework.messaging.converter.MessageConversionException:
Cannot convert from [java.lang.String] to
[com.springboot.model.Student] for GenericMessage
[payload=[com.springboot.model.Student@5e40dc31,
com.springboot.model.Student@235e68b8], headers={kafka_offset=45,
kafka_receivedMessageKey=null, kafka_receivedPartitionId=0,
kafka_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,
kafka_receivedTopic=myTopic-kafkasender}]
配置文件:
@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;
}
}
接收文件:
@Service
public class KafkaReciever {
private static final Logger LOGGER = LoggerFactory.getLogger(KafkaReciever.class);
@KafkaListener(topics = "${kafka.topic.name}", group = "${kafka.consumer.group.id}")
public void recieveData(@Payload Student student, @Header(KafkaHeaders.MESSAGE_KEY) String messageKey) throws Exception{
LOGGER.info("Data - " + student + " recieved");
}
}
POST 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]
Can not deserialize instance of com.springboot.model.Student out of START_ARRAY
如果使用 json 解串器,您有一个列表,而不是单个 Student
@Payload List<Student> student
或者如果使用字符串解串器,你有一个 JSON 的字符串,你必须手动解析它
receiveData(@Payload String student ... ) {
JsonNode data = new ObjectMapper().readTree(student); // for example, but should extract ObjectMapper to a field
}
关于您的其他输出,请参阅
我正在使用 Spring Boot 应用程序通过将其转换为 Kafka Producer 中的 toString() 来发送一个 JSON 数组,但我在 Consumer 中收到以下错误:
org.springframework.kafka.listener.ListenerExecutionFailedException: Listener method could not be invoked with the incoming message Endpoint handler details: Method [public void com.springboot.service.KafkaReciever.recieveData(com.springboot.model.Student,java.lang.String) throws java.lang.Exception] Bean [com.springboot.service.KafkaReciever@5bb3d42d]; nested exception is org.springframework.messaging.converter.MessageConversionException: Cannot handle message; nested exception is org.springframework.messaging.converter.MessageConversionException: Cannot convert from [java.lang.String] to [com.springboot.model.Student] for GenericMessage [payload=[com.springboot.model.Student@5e40dc31, com.springboot.model.Student@235e68b8], headers={kafka_offset=45, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_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, kafka_receivedTopic=myTopic-kafkasender}]
配置文件:
@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;
}
}
接收文件:
@Service
public class KafkaReciever {
private static final Logger LOGGER = LoggerFactory.getLogger(KafkaReciever.class);
@KafkaListener(topics = "${kafka.topic.name}", group = "${kafka.consumer.group.id}")
public void recieveData(@Payload Student student, @Header(KafkaHeaders.MESSAGE_KEY) String messageKey) throws Exception{
LOGGER.info("Data - " + student + " recieved");
}
}
POST 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]
Can not deserialize instance of com.springboot.model.Student out of START_ARRAY
如果使用 json 解串器,您有一个列表,而不是单个 Student
@Payload List<Student> student
或者如果使用字符串解串器,你有一个 JSON 的字符串,你必须手动解析它
receiveData(@Payload String student ... ) {
JsonNode data = new ObjectMapper().readTree(student); // for example, but should extract ObjectMapper to a field
}
关于您的其他输出,请参阅