rest - 如何将 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
}
解决方案
A Set
orHashSet
是一个java概念。Set
从 HTTP 的角度来看,没有 a 这样的东西,在 Postman 中也没有 a 这样的东西Set
。因此,从 Postman 中,您需要以invoice_ids
Spring 的解析库可以转换为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>
推荐阅读
- c++ - 为什么将字符串分配给 const char * 然后将 const char * 作为缓冲区 (void*) 传递给函数时结果不正确?
- javascript - 需要在 $data 中绑定选定的外部数据位
- aframe - 如何使用 AFRAME.TWEEN
- java - 如何使用 Firebase ML 套件识别地标?
- python - 有没有办法将函数参数用作同一函数的另一个参数?
- c++ - 处理dll问题弹出窗口
- python - 将文本数据从flutter发送到flask api并webview返回的html
- reactjs - 找不到 html 容器 - AmCharts4、React 和 Jest 测试
- java - 将我的程序代码转换为面向对象的提示
- c++ - 像这样取消引用 nullptr 是未定义的行为吗?