问题描述
我正在尝试将复杂的搜索查询对象传递给 Spring Controller,而不进行过多的自定义(如自定义转换器)。
例如,我知道我可以得到这样的参数列表/数组:
const compareDate = (dateVal1,dateVal2) => {
if (dateVal1.valueOf() === dateVal2.valueOf()){
return true;
}
else { return false;}
}
GET http://host.com/path?param=abc¶m=123
如果我想要一个对象,我可以这样做:
@GetMapping
String query(String[] param) {
// param[]={abc,123}
...
}
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;
}
如何构造查询参数以正确填充 [...]
class MyObject {
String field1,field2;
nestedobject[] field3; <--
}
[...]
数组?
解决方法
我认为对于复杂的搜索查询最好使用 JSON 并发布。 也许是这样
@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