【问题标题】:consume/return a JSON response when executing flow using Spring boot API使用 Spring Boot API 执行流程时使用/返回 JSON 响应
【发布时间】:2021-03-28 18:13:01
【问题描述】:

我是 Corda 的初学者,我正在尝试使用 Spring Boot API 执行流程。我用的时候:

@PostMapping(value = [ "create-iou" ], 产生 = [ TEXT_PLAIN_VALUE ] , headers = [ "Content-Type=application/x-www-form-urlencoded" ])

我的流程正在执行(通过使用失眠测试它)。但是当我把它改成

@PostMapping(value = [ "create-iou" ], 产生 = [ APPLICATION_JSON_VALUE ], headers = [ "Content-Type=application/json" ])

它给了我一个 406 不可接受的错误:没有返回响应的正文。

这是我创建/复制的 API:

@PostMapping(value = [ "create-iou" ], produces = [ TEXT_PLAIN_VALUE ] , headers = [ "Content-Type=application/x-www-form-urlencoded" ])
fun createIOU(request: HttpServletRequest): ResponseEntity<String> {
    val iouValue = request.getParameter("iouValue").toInt()
    val partyName = request.getParameter("partyName")
        ?: return ResponseEntity.badRequest().body("Query parameter 'partyName' must not be null.\n")
    if (iouValue <= 0 ) {
        return ResponseEntity.badRequest().body("Query parameter 'iouValue' must be non-negative.\n")
    }
    val partyX500Name = CordaX500Name.parse(partyName)
    val otherParty = proxy.wellKnownPartyFromX500Name(partyX500Name) ?: return ResponseEntity.badRequest().body("Party named $partyName cannot be found.\n")

    return try {
        val signedTx = proxy.startTrackedFlow(::Initiator, iouValue, otherParty).returnValue.getOrThrow()
        ResponseEntity.status(HttpStatus.CREATED).body("Transaction id ${signedTx.id} committed to ledger.\n")

    } catch (ex: Throwable) {
        logger.error(ex.message, ex)
        ResponseEntity.badRequest().body(ex.message!!)
    }
}

我想返回这样的东西:

{ 
iouValue: 99, 
lender: PartyA, 
borrower: PartyB
}

使用 http 端点执行流程时。

【问题讨论】:

    标签: spring spring-boot corda


    【解决方案1】:

    需要使用 Corda 提供的 RPC 连接库:

    import net.corda.client.rpc.CordaRPCClient
    import net.corda.client.rpc.CordaRPCConnection
    

    查看this example 了解如何使用它们。

    您没有展示您的代理是如何实例化的,但您需要实例化一个代理以通过 RPC 连接到节点,如下所示:

    val rpcAddress = NetworkHostAndPort(host, rpcPort)
    val rpcClient = CordaRPCClient(rpcAddress)
    val rpcConnection = rpcClient.start(username, password)
    proxy = rpcConnection.proxy
    

    一旦有了代理,就可以创建 SpringBoot API 来调用进行 RPC 调用的代理:

    @RestController
    @RequestMapping("/") 
    class StandardController(rpc: NodeRPCConnection) {
    
        private val proxy = rpc.proxy
        
        @GetMapping(value = ["/addresses"], produces = arrayOf("text/plain"))
        private fun addresses() = proxy.nodeInfo().addresses.toString()
    
        @GetMapping(value = ["/identities"], produces = arrayOf("text/plain"))
        private fun identities() = proxy.nodeInfo().legalIdentities.toString()
    

    【讨论】:

      猜你喜欢
      • 2019-07-27
      • 2021-04-25
      • 2018-08-26
      • 1970-01-01
      • 1970-01-01
      • 2018-02-13
      • 2017-02-17
      • 1970-01-01
      • 2018-07-01
      相关资源
      最近更新 更多