我在消费者方面遇到以下错误:
Kibana
我看不出问题所在。收到的类型与处理程序的类型匹配。映射是可能的...
org.springframework.kafka.KafkaException: No method found for class xxx.web.json.customer.ContractRequestDto
at org.springframework.kafka.listener.adapter.DelegatingInvocableHandler.getHandlerForPayload(DelegatingInvocableHandler.java:170)
还有听众:
@EnableKafka
@Configuration
public class KafkaConfiguration {
@Value(value = "${kafka.servers}")
private String bootstrapAddress;
@Value(value = "${kafka.groups.customer.name}")
private String customerGroup;
@Bean
public ConcurrentKafkaListenerContainerFactory<String, Object> containerFactory() {
ConcurrentKafkaListenerContainerFactory<String, Object> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(new DefaultKafkaConsumerFactory<String, Object>(getConsumerFactoryProperties()));
factory.setConcurrency(5);
return factory;
}
private Map<String, Object> getConsumerFactoryProperties() {
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
props.put(ConsumerConfig.GROUP_ID_CONFIG, customerGroup);
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class.getName());
props.put(JsonDeserializer.TRUSTED_PACKAGES, "*");
return props;
}
}
我的参数用@Payload注释,因此根据文档,它应该可以工作。 你有好主意吗?谢谢。
答案 0 :(得分:0)
我将不得不看一下代码,看看我们是否支持获取完整的ConsumerRecord
及其value()
(有效载荷)。
但是,由于您已经可以通过record.value()
来访问它,因此是多余的,因此我不确定您为什么要这样做。
编辑
我刚刚编写了一个测试应用程序,对我来说很好用;所以正在发生其他事情;也许您的DTO在不同的包装中?
@SpringBootApplication
public class So59970536Application {
public static void main(String[] args) {
SpringApplication.run(So59970536Application.class, args);
}
@Bean
public NewTopic topic() {
return TopicBuilder.name("so59970536").partitions(1).replicas(1).build();
}
@Bean
public ApplicationRunner runner(KafkaTemplate<String, Foo> template) {
return args -> template.send("so59970536", new Foo("bar"));
}
public static class Foo {
private String bar;
public Foo() {
}
public Foo(String bar) {
this.bar = bar;
}
public String getBar() {
return this.bar;
}
public void setBar(String bar) {
this.bar = bar;
}
@Override
public String toString() {
return "Foo [bar=" + this.bar + "]";
}
}
}
@Component
@KafkaListener(id = "so59970536", topics = "so59970536")
class Listener {
@KafkaHandler
public void listen(ConsumerRecord<String, Foo> record, @Payload Foo foo) {
System.out.println(foo);
System.out.println(record);
}
}
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer
spring.kafka.consumer.properties.spring.json.trusted.packages=*
和
Foo [bar = bar]
ConsumerRecord(topic = so59970536,分区= 0,leaderEpoch = 0,偏移量= 2,CreateTime = 1580315933731,序列化密钥大小= -1,序列化值大小= 13,标头= RecordHeaders(标头= [],isReadOnly = false ),键=空,值= Foo [bar = bar])