如何在多个值中获取JSON对象的值,而不是在api方法corda

时间:2017-12-08 10:36:43

标签: corda

我正在尝试使用@GET @Produces api方法在StateandRef中的多个值中仅显示JSON对象值。但是无法从中检索特定值。请帮帮我。

1 个答案:

答案 0 :(得分:0)

以下是IOUState的示例。我们将保险库中的每个IOUState映射到一个简单数据类的实例,然后我们将其作为JSON返回。

IOUStateData数据类

data class IOUStateData(
    val value: Int, 
    val lender: Party, 
    val borrower: Party
)

终端

@GET
@Path("ious")
@Produces(MediaType.APPLICATION_JSON)
fun getIOUs(): Map<String, List<IOUStateData>> {
    // Extract the IOUState StateAndRefs from the vault.
    val iouStateAndRefs = rpcOps.vaultQueryBy<IOUState>().states

    // Map each StateAndRef to its IOUState.
    val iouStates = iouStateAndRefs.map { it.state.data }

    // Map each IOUState to a data class that will be returned as JSON.
    val iouStateDatas = iouStates.map { iouState ->
        IOUStateData(iouState.value, iouState.lender, iouState.borrower)
    }

    return mapOf("iouStates" to iouStateDatas)
}