[英]How to define RequestBody in controller as list, I am getting 500 error
I want to pass search input as list, Refer the below code and payload which i tried, I am getting parser errror.我想将搜索输入作为列表传递,请参考我尝试过的以下代码和有效负载,我收到解析器错误。
// code in searchinput // 搜索输入代码
public class SearchInput {
private List<SearchCriteria> criterias;
}
// code in search criteria // 搜索条件中的代码
public class SearchCriteria {
private String key;
private String operation;
private String value;
}
//code for controller //controller的代码
@PostMapping("/searchhh")
public List<Profile> findProfiles(@RequestBody SearchInput input) {
List<SearchCriteria> criterias = input.getCriterias();
System.out.println("criterias=" + criterias);
return null;
}
// payload which I tired // 我累了的有效载荷
URL:
http://localhost:5555/matrimony/api/v1/profiles/searchhh
Request body:
[
{
"key": "g",
"operation": "eq",
"value": "m"
},
{
"key": "name",
"operation": "like",
"value": "Rani"
},
{
"key": "sh",
"operation": "eq",
"value": "Never"
}
]
Response:
{
"message": "JSON parse error: Cannot deserialize instance of `com.model.SearchInput` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.model.SearchInput` out of START_ARRAY token\n at [Source: (PushbackInputStream); line: 1, column: 1]",
"status": 500,
"timestamp": "2021-01-21T11:31:48.228796"
}
Have you tried this payload:您是否尝试过此有效负载:
{
"criterias" : [
{
"key": "g",
"operation": "eq",
"value": "m"
},
{
"key": "name",
"operation": "like",
"value": "Rani"
},
{
"key": "sh",
"operation": "eq",
"value": "Never"
}
]
}
The above payload which you are passing as request body represents array of SearchCriteria
objects, so you can parse that json directly into List<SearchCriteria>
and no need of SearchInput
class您作为请求正文传递的上述有效负载表示SearchCriteria
对象数组,因此您可以将 json 直接解析为List<SearchCriteria>
而不需要SearchInput
class
@PostMapping("/searchhh")
public List<Profile> findProfiles(@RequestBody List<SearchCriteria> input) {
System.out.println("criterias=" + input);
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.