Corda车险项目CLI中如何传递ClaimInfo对象?求协助
我正在运行Corda的车险示例项目(对应仓库的carinsurance-QueryableState模块),但在CLI中调用InsuranceClaimInitiator流时,不知道如何正确传递claimInfo这个自定义对象类型的参数。
我尝试过两种命令写法:
start InsuranceClaimInitiator claimInfo: {claimNumber: "aa",claimDescription: "rrrr", claimAmount: 9}, policyNumber: "aaa"
以及:
flow start InsuranceClaimFlow$InsuranceClaimInitiator {claimInfo: {claimNmuber: "c001",claimDescription: "hello",claimAmount: 123}}, policyNumber: "hello"
但都触发了错误,比如构造函数匹配失败:
No matching constructor found: - [class net.corda.examples.carinsurance.flows.ClaimInfo, class java.lang.String]: Could not parse as a command: Cannot construct instance of
net.corda.examples.carinsurance.flows.ClaimInfo(no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
还有YAML解析错误:
Expected a field name (Scalar value in YAML), got this instead: <org.yaml.snakeyaml.events.MappingStartEvent(anchor=null, tag=null, implicit=true)>
请问我的传递方式哪里不对?正确的传递方式应该是怎样的?
解决方案
Corda CLI处理自定义对象类型参数时,不能直接用普通键值对格式,必须明确指定对象的全限定类名,并用括号包裹构造参数。另外你之前的命令里还有拼写错误:claimNmuber应该是claimNumber,这也是解析失败的原因之一。
正确的命令示例
针对你的场景,正确的CLI命令应该是:
flow start InsuranceClaimFlow$InsuranceClaimInitiator claimInfo: net.corda.examples.carinsurance.flows.ClaimInfo(claimNumber: "c001", claimDescription: "hello", claimAmount: 123), policyNumber: "hello"
关键注意事项
- 指定完整类路径:自定义对象必须写出完整的包路径+类名,让CLI明确知道要实例化的对象类型,再通过括号传入构造参数(参数名要和类构造函数的参数完全匹配)。
- 修正拼写错误:确保所有参数名和类定义中的字段名一致,比如你之前写错的
claimNmuber要修正为claimNumber。 - 规范参数格式:参数键值对之间用逗号分隔,字符串类型参数加引号,数字直接写即可。
失败原因说明
Corda CLI用YAML解析参数,普通的{key: value}格式仅适用于内置类型(比如Map),无法直接映射到自定义Java/Kotlin对象——因为CLI不知道要把这个Map转换成哪个类的实例。只有通过全限定类名+构造参数的方式,才能明确告诉CLI如何构造目标对象。
内容的提问来源于stack exchange,提问作者adnan ahmed

