1

So I'm using Spring Boot in Java to write my API. I have a request coming in as such and (NOTE: there will be an indeterminate number of fields coming in, so this may be 100 to where a "field99=test99" will be part of the request):

www.example.com/api/examples?field=test&field1=test1&field2=test2

All of my request properties will have the name "field" in it, but I have no clue what number will be in the suffix. I currently have in my API the following parameter:

@RequestParam(value = "field", required = false) List<String> fields

Is there a wildcard matching situation that I could utilize here to make this work? It seems way too tedious/unrealistic to create 100 parameters called String field, String field1, String field2... How would I go about solving this problem so that all the request properties' values end up in my fields list (test, test1, test2, ...)?

  • There is some useful information here: https://stackoverflow.com/questions/6243051/how-to-pass-an-array-within-a-query-string – Matt Vickery Dec 26 '21 at 08:39

1 Answers1

1

Probably the best will be to map all request parameters to a Map and then parse the keys for your prefix:

@GetMapping("/examples")
@ResponseBody
public String examples(@RequestParam Map<String,String> allParams) {
    for(Object key : allParams.keySet()) {
        if (key.toString().matches("field[0-9]*")) {

https://www.baeldung.com/spring-request-param#mapping-all-parameters

PeterMmm
  • 24,152
  • 13
  • 73
  • 111