1

I'm trying to pass a complex search query object to a Spring Controller, without making too much customization (like custom converters).

I know, for instance, that I can get a list / array of parameters like this:

GET http://host.com/path?param=abc&param=123
@GetMapping
String query(String[] param) {
    // param[]={abc, 123}
    ...
}

And if I want an object I can do this:

GET http://host.com/path?field1=abc&field2=123&field3.a=1&field3.b=2
@GetMapping
String query(MyObject obj) {
    // MyObject(field1=abc, field2=123, field3=NestedObject(a=1, b=2))
    ...
}

class MyObject {
    String field1, field2;
    NestedObject field3;
}

class NestedObject {
    int a, b;
}

But what I really need is to combine both:

[...]
class MyObject {
    String field1, field2;
    NestedObject[] field3; <--
}
[...]

How do I structure the query parameters to correctly fill the array of NestedObject?

Lucas Noetzold
  • 1,670
  • 1
  • 13
  • 29
  • See https://stackoverflow.com/questions/59256378/spring-rest-binding-get-parameters-to-list-of-nested-objects – ltsallas Jun 03 '21 at 20:14

1 Answers1

0

I think it's better to use JSON and post for a complex search query. Maybe like this

@RequestMapping(value = "/greeting", method = POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@ResponseBody
public String greetingJson(HttpEntity<String> httpEntity) {
    String json = httpEntity.getBody();
    // json contains the plain json string
Inanc Cakil
  • 306
  • 2
  • 9