简体   繁体   中英

Spring Kafka "cannot access com.fasterxml.jackson.core.type.TypeReference"

I would like to receive a json using a kafka consumer. 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 .

First, I produce some payload using 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"}

Then, I have a class with the @KafkaListener annotation:

@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));
    }
}

The consumer has the following config:

@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;
    }

And the 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;
    }
}

Add this dependency:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM