gpt4 book ai didi

java - Scala 类 Final def this() 声明没有公共(public)无参构造函数

转载 作者:行者123 更新时间:2023-12-02 03:34:13 26 4
gpt4 key购买 nike

尝试构建一个使用 Jackson Smile 2.6.6 和 jackson-module-scala 的通用 Scala 2.11 Jackson 序列化器。不管出于什么原因,Java 找不到公共(public)的无参数构造函数。尽管下面的类可以编译,但我在运行时从 Kafka 客户端收到以下错误:

Caused by: org.apache.kafka.common.KafkaException: Could not instantiate class ai.bernie.api.util.serialization.kafka.JacksonGenericSerializer Does it have a public no-argument constructor?
at org.apache.kafka.common.utils.Utils.newInstance(Utils.java:318) ~[kafka-clients-0.10.0.0.jar:na]
at org.apache.kafka.common.config.AbstractConfig.getConfiguredInstance(AbstractConfig.java:201) ~[kafka-clients-0.10.0.0.jar:na]
at org.apache.kafka.clients.consumer.KafkaConsumer.<init>(KafkaConsumer.java:671) ~[kafka-clients-0.10.0.0.jar:na]
... 27 common frames omitted
Caused by: java.lang.InstantiationException: ai.bernie.api.util.serialization.kafka.JacksonGenericSerializer
at java.lang.Class.newInstance(Class.java:427) ~[na:1.8.0_51]
at org.apache.kafka.common.utils.Utils.newInstance(Utils.java:314) ~[kafka-clients-0.10.0.0.jar:na]
... 29 common frames omitted
Caused by: java.lang.NoSuchMethodException: ai.bernie.api.util.serialization.kafka.JacksonGenericSerializer.<init>()
at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_51]
at java.lang.Class.newInstance(Class.java:412) ~[na:1.8.0_51]
... 30 common frames omitted

这是类,我该如何修复它?我尝试了使用 final def 并将私有(private) var 移入类中的各种组合,尽管我可能做错了,但也尝试使用无参数 apply 创建一个伴生对象 方法。

class JacksonGenericSerializer[T: Manifest] private (
private var mapper: ObjectMapper with ScalaObjectMapper = null
) extends Closeable with AutoCloseable with Serializer[T] with Deserializer[T] {

final def this() = this(new ObjectMapper(new SmileFactory) with ScalaObjectMapper)

override def configure(map: java.util.Map[String, _], b: Boolean) {
if (mapper == null) {
mapper = new ObjectMapper(new SmileFactory) with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
}
}

override def serialize(s: String, record: T): Array[Byte] = {
try {
mapper.writeValueAsBytes(record)
}
catch {
case e: JsonProcessingException => {
throw new IllegalArgumentException(e)
}
}
}

override def deserialize(s: String, bytes: Array[Byte]): T = {
try {
mapper.readValue[T](bytes)
}
catch {
case e: IOException => {
throw new IllegalArgumentException(e)
}
}
}

override def close {
mapper = null
}
}

编辑

我还应该提到,我正在使用 Google Guice,尽管我怀疑它会影响这一点,因为序列化类本身没有被注入(inject),只有使用它的 Kafka Consumer/Producer 父类。

最佳答案

因为 JacksonGenericSerializer 类型参数具有上下文绑定(bind)(即 [T: Manifest]),辅助“无参数”构造函数实际上采用一个参数:Manifest [T]

您可以使用 javap 确认这一点:

$ javap JacksonGenericSerializer.class

Compiled from "JacksonGenericSerializer.scala"
public class JacksonGenericSerializer<T> implements java.io.Closeable, java.lang.AutoCloseable, org.apache.kafka.common.serialization.Serializer<T>, org.apache.kafka.common.serialization.Deserializer<T> {
public void configure(java.util.Map<java.lang.String, ?>, boolean);
public byte[] serialize(java.lang.String, T);
public T deserialize(java.lang.String, byte[]);
public void close();
public JacksonGenericSerializer(scala.reflect.Manifest<T>);
}

这个问题涉及类似的问题:How to create new instance of Scala class with context bound via Java reflection using only zero argument constructor?

关于java - Scala 类 Final def this() 声明没有公共(public)无参构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37635416/

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