gpt4 book ai didi

java - Spring-Kafka : Issue while deserialising kafka message - class not in a "trusted package"?

转载 作者:行者123 更新时间:2023-11-30 01:57:19 29 4
gpt4 key购买 nike

我收到以下异常,因为我从一个项目生产,而消费者从另一个项目消费。我怎样才能解决这个问题。显然,这些包不一样。那么如何确保 json 序列化正确。

The class 'com.lte.assessment.assessments.AssessmentAttemptRequest' is not in the trusted packages: [java.util, java.lang, com.lte.assessmentanalytics.model

消费者配置

@EnableKafka
@Configuration
public class KafkaConfig {
static Map<String, Object> config = new HashMap();

static {
config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
config.put(ConsumerConfig.GROUP_ID_CONFIG, "group_id");
config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
}


@Bean
public ConsumerFactory<String, AssessmentAttemptRequest> assessmentAttemptDetailsEntityConsumerFactory() {
JsonDeserializer<AssessmentAttemptRequest> deserializer = new JsonDeserializer<>();
deserializer.addTrustedPackages("com.lte.assessment.assessments");
return new DefaultKafkaConsumerFactory(config, new StringDeserializer(), deserializer);
}

}

生产者配置

@Configuration
public class KafkaConfiguration {

@Bean
public ProducerFactory producerConfig() {
Map<String, Object> config = new HashMap();

config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);

return new DefaultKafkaProducerFactory(config);
}

@Bean
public KafkaTemplate kafkaTemplate() {
return new KafkaTemplate(producerConfig());
}

@Bean
public ConcurrentKafkaListenerContainerFactory aaKafkaListenerFactory() {
ConcurrentKafkaListenerContainerFactory<String, AssessmentAttemptDetailsEntity> factory = new ConcurrentKafkaListenerContainerFactory();
factory.setConsumerFactory(assessmentAttemptDetailsEntityConsumerFactory());
return factory;
}
}

最佳答案

您可以通过更改 assessmentAttemptDetailsEntityConsumerFactory() 来将您的包列入白名单,如下所示:

    @Bean
public ConsumerFactory<String, AssessmentAttemptDetailsEntity> assessmentAttemptDetailsEntityConsumerFactory() {
JsonDeserializer<AssessmentAttemptDetailsEntity>
deserializer = new JsonDeserializer<>();
deserializer.addTrustedPackages("com.lte.assessment.assessments");//your package
return new DefaultKafkaConsumerFactory(config,deserializer);
}

关于java - Spring-Kafka : Issue while deserialising kafka message - class not in a "trusted package"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53964530/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com