Class 没有注释,也没有在白名单中,不能用于序列化

Class is not annotated or on the whitelist, so cannot be used in serialization

在Corda中,我定义了以下流程:

object Flow {
    @InitiatingFlow
    @StartableByRPC
    class Initiator(val otherParty: Party) : FlowLogic<Unit>() {
        override val progressTracker = ProgressTracker()

        @Suspendable
        override fun call() {
            val otherPartyFlow = initiateFlow(otherParty)
            otherPartyFlow.send(MyClass())
        }
    }

    @InitiatedBy(Initiator::class)
    class Acceptor(val otherPartyFlow: FlowSession) : FlowLogic<Unit>() {
        @Suspendable
        override fun call() {
            val unregisteredClassInstance = otherPartyFlow.receive<MyClass>()
        }
    }
}

但是,当我尝试 运行 流程时,出现以下错误:

Class com.example.flow.MyClass is not annotated or on the whitelist, so cannot be used in serialization

如何将 class 注释或列入白名单以允许它在流程中发送?为什么我需要这样做?

默认情况下,出于安全目的,只有 default serialization whitelist 上的 classes 可以在流内或通过 RPC 发送。

有两种方法可以将特定的class添加到序列化白名单:

1.将 class 注释为 @CordaSerializable:

@CordaSerializable
class MyClass

2。创建序列化白名单插件:

定义一个序列化插件如下:

class TemplateSerializationWhitelist : SerializationWhitelist {
    override val whitelist: List<Class<*>> = listOf(MyClass::class.java)
}

然后列出序列化白名单插件的完全限定 class 名称(例如 com.example.TemplateSerializationWhitelist 在您的 CorDapp src/main/resources/META-INF/services 文件夹中名为 net.corda.core.serialization.SerializationWhitelist 的文件中。

为什么添加一个class到序列化白名单有两种方法?

第一种方法更简单,但如果您无法将注释添加到要发送的 class 中,则无法实现。