[英]why com.fasterxml.jackson.core.type.TypeReference implements Comparable interface?
[英]Spring Kafka "cannot access com.fasterxml.jackson.core.type.TypeReference"
我想使用卡夫卡消费者收到 json。 Unfortunately, I get the following error: java: cannot access com.fasterxml.jackson.core.type.TypeReference class file for com.fasterxml.jackson.core.type.TypeReference not found
.
首先,我使用 kafka CLI 生成一些有效载荷:
kafka-console-producer --broker-list localhost:9092 --topic t2
>{"payload":"a", "appId":"b", "userId":"3", "deviceType":"d"}
>{"payload":"aa", "appId":"bb", "userId":"44", "deviceType":"e"}
然后,我有一个带有@KafkaListener
注释的 class :
@Service
public class SimpleConsumerService {
public static final Logger LOGGER = LogManager.getLogger(SimpleConsumerService.class);
@KafkaListener(topics = "t2")
public void consumer(SingleUserRequest singleUserRequest) {
LOGGER.info(String.format("Req received: %s", singleUserRequest));
}
}
消费者具有以下配置:
@Configuration
@EnableKafka
public class KafkaConfig {
@Autowired
KafkaProperties kafkaProperties;
private Map<String, Object> getConsumerProps() {
var consumerConfig = new HashMap<>(
kafkaProperties.buildConsumerProperties()
);
consumerConfig.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
consumerConfig.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
consumerConfig.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
consumerConfig.put(ConsumerConfig.GROUP_ID_CONFIG, "g1");
consumerConfig.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
consumerConfig.put(ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG, 60000);
consumerConfig.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
return consumerConfig;
}
@Bean
public ConsumerFactory<String, SingleUserRequest> consumerFactory() {
return new DefaultKafkaConsumerFactory<>(getConsumerProps(), new StringDeserializer(),
new JsonDeserializer<>(SingleUserRequest.class));
}
@Bean
public ConcurrentKafkaListenerContainerFactory<String, SingleUserRequest> kafkaListenerContainerFactory() {
var factory = new ConcurrentKafkaListenerContainerFactory<String, SingleUserRequest>();
factory.setConsumerFactory(consumerFactory());
return factory;
}
和SingleUserRequest
:
public class SingleUserRequest {
String payload;
String appId;
String userId;
String deviceType;
public SingleUserRequest() {
}
public SingleUserRequest(String payload, String appId, String userId, String deviceType) {
this.payload = payload;
this.appId = appId;
this.userId = userId;
this.deviceType = deviceType;
}
public String getPayload() {
return payload;
}
public String getAppId() {
return appId;
}
public String getUserId() {
return userId;
}
public String getDeviceType() {
return deviceType;
}
}
添加此依赖项:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.