我有REST API,我想在一些api调用中使用Kafka流。我能够将代码部署到JBoss服务器而没有任何错误,但是执行api调用时出现以下错误:
org.jboss.resteasy.spi.UnhandledException: java.lang.NoClassDefFoundError: org / apache / kafka / clients / consumer / KafkaConsumer
我可以在IntelliJ的外部库中看到该类。这是我的资源类:
package com.company.rest;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.Properties;
import java.util.Arrays;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import java.util.function.Supplier;
@Path("topic")
public class TopicResource {
private final static String TOPIC = "MyTopic";
private final static String BOOTSTRAP_SERVERS = "localhost:9092";
@GET
@Path("/{name}")
@Produces(MediaType.TEXT_PLAIN)
public void getTopic(@PathParam("name") String name) {
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "SupplierDeserializer");
KafkaConsumer<String, Supplier> consumer = new KafkaConsumer<String, Supplier>(props);
consumer.subscribe(Arrays.asList("MyTopic"));
while (true){
ConsumerRecords<String, Supplier> records = consumer.poll(100);
for (ConsumerRecord<String, Supplier> record : records){
System.out.println("Print some text");
}
}
}
}
这是我的pom.xml文件:
<dependencies>
<!-- https://mvnrepository.com/artifact/javax.ejb/javax.ejb-api -->
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>javax.ejb-api</artifactId>
<version>3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-streams -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
我不确定我做错了什么还是与某些IntelliJ配置有关。任何帮助将不胜感激。 预先感谢!