如何将HashSet传递给服务器以测试邮递员的API?

问题描述 投票:0回答:1

我创建了一个我想用邮递员测试的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

}
java rest spring-boot java-api
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.