How to send 1 to many parameters to a Rest Api using java

Viewed 29

I'm building a rest API that should accept any number of parameters that I pass into it. Is there a way to configure this in Java?

Below I have a REST API method with 2 parameters, but I'd like it to accept just 1 or up to any number (50 for example).

@GetMapping("/rest/bagel")
public Collection<String> bagel(
        @RequestParam(value = REQUEST_PARAM_BAGEL_ORG) String bagelOrg, 
        @RequestParam(value = REQUEST_PARAM_BAGEL_REGION) String bagelRegion) {
    return bagelService.getBagel(bagelOrg, bagelRegion);
}
1 Answers

Use Map<String, String> as @RequestParam. You will get all param name - value pair in map

public Collection<String> bagel(@RequestParam Map<String, String> paramMap){
    ...
}

You can send param this way

...rest/bagel?bagelOrg=mgr&bagelRegion=region

Then your map will be

paramMap= {bagelOrg=mgr, bagelRegion=region}
Related