Spring启动无法加载属性。以下是我通过yaml文件使用的属性。
spring:
kafka:
bootstrap-servers: localhost:9092
consumer:
auto-commit-interval: 100
enable-auto-commit: true
group-id: ********************
auto-offset-reset: earliest
value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
producer:
batch-size: 16384
buffer-memory: 33554432
retries: 0
value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
listener:
poll-timeout: 20000
我得到的例外是这个
引起:java.lang.IllegalAccessException:类org.apache.kafka.common.utils.Utils无法使用修饰符&#34访问org.springframework.kafka.support.serializer.JsonDeserializer类的成员被保护的"
我认为构造函数受到保护。请提供一种实例化方法。
答案 0 :(得分:0)
那是对的。参见:
protected JsonDeserializer() {
this((Class<T>) null);
}
protected JsonDeserializer(ObjectMapper objectMapper) {
this(null, objectMapper);
}
public JsonDeserializer(Class<T> targetType) {
this(targetType, new ObjectMapper());
this.objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
this.objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
JsonDeserializer
并非设计为由默认构造函数实例化,因为它需要知道要反序列化的targetType
。
您可以将此类扩展为您的特定类型:
public class FooJsonDeserializer extends JsonDeserializer<Foo> { }
并将此作为该value-deserializer
属性的类值使用。
或者您可以考虑自定义DefaultKafkaConsumerFactory
:
@Bean
public ConsumerFactory<?, ?> kafkaConsumerFactory(KafkaProperties properties) {
Map<String, Object> consumerProperties = properties.buildConsumerProperties();
consumerProperties.put(CommonClientConfigs.METRIC_REPORTER_CLASSES_CONFIG,
MyConsumerMetricsReporter.class);
DefaultKafkaConsumerFactory<Object, Object> consumerFactory =
new DefaultKafkaConsumerFactory<>(consumerProperties);
consumerFactory.setValueDeserializer(new JsonDeserializer<>(Foo.class));
return consumerFactory;
}