Home > Back-end >  Setting multiple parameters in a request
Setting multiple parameters in a request

Time:10-05

I need to form a cache, for regions by id, which are passed in the region_ids parameter, the request looks like this:

localhost:8080/cache/mscache?region_ids=5,19....,23

how is it best to read these several parameters in the program code?

CodePudding user response:

read them a String and the parse that String into whatever you want:

@GetMapping("/cache/mscache)
public String getCache(@RequestParam String listOfRegionIds)
    List<String> ids = Arrays.stream(listOfRegiosIds.split(",")).collect(Collectors.toList);
// ...
}

more info at https://www.baeldung.com/spring-request-param

CodePudding user response:

You can use an array or a List

    @GetMapping(value = "/test")
    public void test(@RequestParam List<String> ids) {
        ids.forEach(System.out::println);
    }

Make a get request like:

http://localhost:8080/test?ids=1,2,3

Check here for more details.

CodePudding user response:

If the request is a Get request use @RequestParam like sugessted by J Asgarov, if it is soething else you can also use @RequestBody by creating an class containing all of your parameters

for exemple a Post request could look like this :

@PostMapping("...")
public String postCache(@RequestBody RegionIdsRequest regionIds)
// ...
}

public class RegionIdsRequest{
    List<int> regionsIds = //...
}
  • Related