moud
moud

Reputation: 61

How do I pass a list of lists as @RequestParam?

Desired request:

http://localhost:8080/values?input=[["aaa","bbb","ccc","ddd"],["abcd","abcd","abcd","abcd"]]

I would love to be able to pass the URL in some way using brackets.

However if it is not possible, how do I still pass the list of lists ?

My controller path:

@GetMapping(path = "/values/")
public String getValues(@RequestParam List<List<@Size(min=4, max=4)String>> input) {
    return input.get(0).get(2);
}

which should return ccc.

Upvotes: 0

Views: 3051

Answers (2)

user404
user404

Reputation: 2028

This is not a good way to do this job. Instead you can pass them as request parameter and and receive them from hashMap. But since you are passing list of lists, so it would be convenient to pass them in request body. To do that you have to create a request POJO class following these steps:

public class ListRequest{
    private List<List<String>> inputList;
    //generate getter, setter for it
}

Now, change in your controller, replace GET method with POST:

@PostMapping(path = "/values/")
    public String getValues(@RequestBody ListRequest input) {
         List<List<String>> yourData=input;
         System.out.println(yourData);
        //operate on your data as you wish
        return input.get(0).get(2);
    }

Upvotes: 4

Sara
Sara

Reputation: 623

Create a Pojo class with a list of list as field. Send the values in the request body and use a post method. In the controller method, use this Pojo object to retrieve those values.

Upvotes: 1

Related Questions