注释属性KafkaListener.topics必须是数组初始值设定项或常量表达式(annotation attribute KafkaListener.topics must be an array

编程入门 行业动态 更新时间:2024-10-24 13:17:50
注释属性KafkaListener.topics必须是数组初始值设定项或常量表达式(annotation attribute KafkaListener.topics must be an array initializer or Constant Expression)

我目前使用Spring Kafka API

我们目前有要求为Kafka主题动态添加后缀。 在尝试修改并将其设置回Annotation“@KafkaListener”时,如下所示 -

@KafkaListener(topics = modifiedTopics)

它给了我一个错误,说 - 注释属性KafkaListener.topics的值必须是数组初始值设定项

或者如果我尝试下面的代码 - (用花括号)

@KafkaListener(topics = {modifiedTopics})

它给了我错误说 - 注释属性KafkaListener.topics的值必须是一个常量表达式

有没有人遇到与annotataions类似的问题? 如果是,请提供帮助!

I am currently using Spring Kafka API's

We currently have a requirement to dynamically add a suffix to Kafka topics. While trying to modify and set it back to Annotation "@KafkaListener" as follows -

@KafkaListener(topics = modifiedTopics )

It gives me an error saying - The value for annotation attribute KafkaListener.topics must be an array initializer

or if I try the code one below - (with flower braces)

@KafkaListener(topics = {modifiedTopics} )

It gives me error saying - The value for annotation attribute KafkaListener.topics must be a constant expression

Has anyone faced similar issues with annotataions? If yes, kindly help!

最满意答案

解决方案是:将lambda添加到注释中的一种方法如下:在KafkaReceiver类的方法中 -

@Autowired TopicUtil topicUtil; @KafkaListener(topics = "#{topicUtil.suffixTopics()}") //In the TopicUtil - add the follwoing method public String[] suffixTopics() { return Arrays.asList(pTopics.split(",")).stream().map(p -> p + envSuffix).toArray(String[]::new); }

Solution is: One of the way to add lambda into annotation is as follows: In the KafkaReceiver class's method -

@Autowired TopicUtil topicUtil; @KafkaListener(topics = "#{topicUtil.suffixTopics()}") //In the TopicUtil - add the follwoing method public String[] suffixTopics() { return Arrays.asList(pTopics.split(",")).stream().map(p -> p + envSuffix).toArray(String[]::new); }

更多推荐

本文发布于:2023-07-19 09:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1176917.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常量   数组   表达式   注释   属性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!