首页 > 解决方案 > 如何将 HashSet 传递给服务器以测试邮递员的 API?

问题描述

我创建了一个我想使用邮递员测试的 API。我的 api 接受许多参数,其中一个参数是 HashSet。我不知道如何使用邮递员传递 HashSet 参数。请帮我。提前致谢

这是我的代码:

@PutMapping
    @ApiOperation(value = "collectMultiInvoices", nickname = "collectMultiInvoices")
    public BaseResponse collectAmountMultipleInvoices(@RequestParam(value = "invoice_id") HashSet<Integer> invoiceIds,
                                      @RequestParam("date") String _date,
                                      @RequestParam(value = "cash", required = false) Float cashAmount,
                                      @RequestParam(value = "chequeAmount", required = false) Float chequeAmount,
                                      @RequestParam(value = "chequeNumber", required = false) String chequeNumber,
                                      @RequestParam(value = "chequeDate", required = false) String _chequeDate,
                                      @RequestParam(value = "chequeImage", required = false) MultipartFile chequeImage,
                                      @RequestParam(value = "chequeBankName", required = false) String chequeBankName,
                                      @RequestParam(value = "chequeBankBranch", required = false) String chequeBankBranch,
                                      @RequestParam(value = "otherPaymentAmount", required = false) Float otherPaymentAmount,
                                      @RequestParam(value = "otherPaymentType", required = false) Integer otherPaymentType,
                                      @RequestParam(value = "otherPaymentTransactionId", required = false) String otherPaymentTransactionId,
                                      @RequestParam(value = "discountPercentorAmount", required = false) String discountPercentorAmount,
                                      @RequestParam(value = "discountId", required = false) String discountId) throws AppException.RequestFieldError, AppException.CollectionAmountMoreThanOutstanding {


//method implementation

}

标签: restspring-bootjava

解决方案


A SetorHashSet是一个java概念。Set从 HTTP 的角度来看,没有 a 这样的东西,在 Postman 中也没有 a 这样的东西Set。因此,从 Postman 中,您需要以invoice_idsSpring 的解析库可以转换为HashSet. 正如@Michael 在评论中指出的那样,一种方法是invoice_id像这样用逗号分隔 s: invoice_id=id1,id2,id3。当 Spring 处理这个请求时,它会看到你在期待 a 形式的数据HashSet,所以它会尝试转换id1,id2,id3成 a HashSet<Integer>,它知道如何自动执行。

旁注:除非您特别需要 a HashSet,否则使用接口而不是实现子类来声明您的类型被认为是一种好习惯。因此,在这种情况下,我建议将您的方法签名更改为接受 aSet<Integer>而不是 aHashSet<Integer>


推荐阅读