[英]How to pass HashSet to server to test API from postman?
I created an API which I want to test using postman. 我创建了一个我想用邮递员测试的API。 My api is accepting many parameters and one parameter is HAshSet.
我的api接受了很多参数,一个参数是HAshSet。 I dont know how to pass HashSet parameter using postman.
我不知道如何使用邮递员传递HashSet参数。 Please help me.
请帮我。 Thanks in advance
提前致谢
Here is my code: 这是我的代码:
@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
or HashSet
is a java concept. Set
或HashSet
是一个java概念。 There is no such thing as a Set
from the HTTP perspective, and there is no such thing as a Set
in Postman. 有一个这样的东西
Set
从HTTP的角度来看,并没有作为一个没有这样的东西Set
在邮差。 So from Postman, you need to send the invoice_ids
in a format that Spring's parsing library can convert to a HashSet
. 所以从Postman,您需要以Spring的解析库可以转换为
HashSet
的格式发送invoice_ids
。 As @Michael pointed out in the comments, one way to do this is to comma separate the invoice_id
s like this: invoice_id=id1,id2,id3
. 正如@Michael在评论中指出的那样,一种方法是用逗号分隔
invoice_id
如下所示: invoice_id=id1,id2,id3
。 When Spring processes this request, it will see that you are expecting data in the form of a HashSet
, so it will attempt to convert id1,id2,id3
into a HashSet<Integer>
, which it knows how to do automatically. 当Spring处理这个请求时,它会看到你期望
HashSet
形式的数据,所以它会尝试将id1,id2,id3
转换为HashSet<Integer>
,它知道如何自动执行。
Side note: Unless you specifically need a HashSet
, it is considered good practice to declare your type using the interface instead of an implementing subclass. 旁注:除非您特别需要
HashSet
,否则使用接口而不是实现子类声明类型被认为是一种好习惯。 So in this situation I would recommend changing your method signature to accept a Set<Integer>
instead of a HashSet<Integer>
所以在这种情况下,我建议更改方法签名以接受
Set<Integer>
而不是HashSet<Integer>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.