Skip to content
Advertisement

Is it possible to split request params in Spring controllers?

I have a request like:

JavaScript

I would like to split those params to bind a List<String> sort in my controller or List<SortParam> where SortParam is the class with fields like: name (String) and ask (boolean).

So the final controller would look like this:

JavaScript

or

JavaScript

Is there a way to make it?

UPDATE:

The standard way of passing parameters does not satisfy my requirements. I.e. I cannot use sort=myfield1&sort=-myfield2&sort=myfield3. I have to use comma separated names.
Also, I do understand that I can accept @RequestParam String sort in my controller and then split the string inside the controller like sort.split(",") but it also doesn’t solve the above problem.

Advertisement

Answer

Yes, you can certainly do that, you’re almost there.

JavaScript

You should now be able to call your service like this (if search is located at your root, otherwise adapt according to your situation):

JavaScript

Hope this helps!

EDIT Sorry, I have read your comments and what you need is clear to me now. I have created a workaround for you that is almost what you want I think.

So first a SortParams class:

JavaScript

Then your controller could look like this:

JavaScript

Now your SortParams object has a list of SortParam:

JavaScript

Would something like this fit what you’re looking for?

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement