1

I have a resource I want to filter by some search terms, among which there are some attributes which I'd like to send in the format

?attributeNames[0]=Foo&attributeValues[0]=Bar&attributeValues[0]=Baz

(or semantically equivalent - each name can map to one or more values)

What's the best way to represent this inside a Spring REST endpoint? I've tried with method parameters like

public void endpoint(@RequestParam(...)String[] names, @RequestParam(...)String[][] values) {
}

but this doesn't seem to work as expected (I can't see any way to specify the parameter names correctly). Is it possible to do this or is it just going to work out better if I use some other mechanism (serialize the parameters as JSON objects for example)?

Chris Cooper
  • 869
  • 2
  • 18
  • 42
  • Request params have their own limitations, specially when it comes to arrays. As the given answer states, it's better to pass them as request body in this cases. – Aritz Jun 24 '16 at 09:47
  • Possible duplicate of [Can Spring MVC handle multivalue query parameter?](http://stackoverflow.com/questions/9768509/can-spring-mvc-handle-multivalue-query-parameter) – Daniel Figueroa Jun 24 '16 at 09:54
  • @DanielFigueroa that's close, but I need to reference the index of one parameter from the other. – Chris Cooper Jun 24 '16 at 13:20
  • Spring 3+ supports @RequestParam with a MultiValueMap allowing you to repeat query param keys as you have done above. Also see https://stackoverflow.com/questions/22398892/how-to-capture-multiple-parameters-using-requestparam-using-spring-mvc – chrisinmtown Jan 16 '18 at 20:00

1 Answers1

1

Technically you can implement this as just one query parameter, since everything after ? can be interpreted as one parameter.

You can then handle it programmatically inside your endpoint.

But this is not a recommended way because you will end up exposing a lot of stuff in the URL. Better way would be to handle it in a request body as a Mutlipart or Form parameter

Sampada
  • 2,931
  • 7
  • 27
  • 39