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?

1

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.