You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Groovy技术求助:如何解决No signature of method调用错误

问题解决:Groovy Kafka消费者脚本报错String.call()

错误原因

你代码里调用了StringDeserializer.name(),但StringDeserializer是普通Java类(不是枚举),根本不存在静态name()方法。Groovy在解析这个错误调用时,触发了java.lang.String.call()的异常。另外,代码中使用的Duration.ofMillis(100)也缺少必要的导入,Groovy不会自动导入java.time.Duration类。

修正方案

  1. 替换错误的类名获取方式:把StringDeserializer.name()改成StringDeserializer.class.name(Groovy简化写法)或者StringDeserializer.class.getName()(标准Java写法)。
  2. 添加java.time.Duration的导入语句。

修正后的完整代码

@Grab(group='org.apache.kafka', module='kafka-clients', version='2.8.0')
import org.apache.kafka.clients.consumer.KafkaConsumer
import org.apache.kafka.common.serialization.StringDeserializer
import java.time.Duration  // 添加这个导入

def props = new Properties()
props.setProperty("bootstrap.servers", "localhost:9092")
props.setProperty("group.id", "sample-group")
// 替换name()为class.name
props.setProperty("key.deserializer", StringDeserializer.class.name)
props.setProperty("value.deserializer", StringDeserializer.class.name)

def consumer = new KafkaConsumer<String, String>(props)
consumer.subscribe(["test-topic"])

while (true) {
  def records = consumer.poll(Duration.ofMillis(100))
  for (def record : records) {
    println("Received message: ${record.value()}")
  }
}

内容的提问来源于stack exchange,提问作者Goku

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 12:55:43