我想将XMLEncoder / XMLDecoder的序列化支持添加到Scala中的case类。这是我的尝试:
@ConstructorProperties(Array("sequenceNumber", "nanosecondsTotal", "bytesTotal", "timeMillis"))
final case class IoStatistics(
@BeanProperty sequenceNumber: Long,
@BeanProperty nanosecondsTotal: Long,
@BeanProperty bytesTotal: Long,
@BeanProperty timeMillis: Long = System.currentTimeMillis
) {
...
}
我可以用javap验证生成的类确实有公共四参数构造函数和属性的相应bean getter。但是,在尝试使用XMLEncoder
序列化实例时,我得到以下控制台输出:
java.lang.InstantiationException: net.java.truevfs.ext.jmx.model.IoStatistics
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(IoStatistics);
Continuing ...
,输出为:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.7.0_06" class="java.beans.XMLDecoder">
</java>
似乎注释不适用于构造函数。我该如何验证并解决此问题?
答案 0 :(得分:1)
我对XML序列化一无所知,但是你应该将ConstructorProperties
注释应用于构造函数而不是类?
case class Foo @ConstructorProperties(Array("sequenceNumber", "nanosecondsTotal", "bytesTotal", "timeMillis"))(
@BeanProperty sequenceNumber: Long,
@BeanProperty nanosecondsTotal: Long,
@BeanProperty bytesTotal: Long,
@BeanProperty timeMillis: Long = System.currentTimeMillis
) {
//...
}