如何在Postman中向接收HashSet参数的API发起测试请求?
HashSet<Integer> Parameter (invoice_id) in Postman for Your Spring Boot API Hey there! I get it, figuring out how to send collection-type parameters in Postman can be tricky at first. Let me break down exactly how to test your collectMultiInvoices API properly.
Since your API uses @RequestParam for the invoice_id parameter (typed as HashSet<Integer>), Spring MVC makes this straightforward—you just need to pass the parameter multiple times (once for each integer in your set) in the request's query parameters. Here's step-by-step:
- Open Postman and load your PUT request for the API endpoint.
- Switch to the Params tab (this is where query parameters live).
- Add the
invoice_idparameter repeatedly for each integer you want to include:- First, enter
invoice_idas the key, and your first integer value (e.g.,123) as the value. - Click the Add button to create a new parameter row, again use
invoice_idas the key, and enter your next integer (e.g.,456). - Repeat this for every integer you need in the
HashSet.
- First, enter
- Add all your other required/optional parameters (like
date,cash,chequeAmount, etc.) in the same Params tab just like you would with any regular parameter. - Send the request—Spring will automatically collect all the
invoice_idvalues into aHashSet<Integer>and pass it to yourinvoiceIdsparameter.
Alternative: Comma-Separated Values (If Configured)
If you prefer, you can also pass all integers in a single invoice_id parameter separated by commas (e.g., invoice_id=123,456,789). For this to work, you might need to add a small configuration to your Spring Boot app to enable comma-separated value parsing for collections:
@Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry registry) { registry.addConverter(String.class, HashSet.class, source -> new HashSet<>(Arrays.asList(source.split(","))) ); } }
That said, the first method (repeating the parameter) is the default behavior and doesn't require any extra configuration, so it's the simpler route.
Just to confirm, your existing controller code is set up correctly to handle this—Spring's @RequestParam automatically maps multiple query parameters with the same name to a collection type like HashSet.
内容的提问来源于stack exchange,提问作者Ankit Srivastava

