[英]How to validate requestbody which contains multiple valid json requests?
When I supply multiple json request under request body for POST request, very first request gets accepted while rest of gets ignored. 当我在请求主体下为POST请求提供多个json请求时,第一个请求被接受,其余请求被忽略。
I don't want to send list as request infact I am supplying multiple requests or say duplicate request in requestbody. 我不想发送列表作为请求事实,我正在提供多个请求或在请求正文中说重复的请求。
Here is an example: 这是一个例子:
Request: 请求:
{
"Business": {
"name": "ABC"
}
}
{
"Business": {
"name": "XYZ"
}
}
Controller's method: 控制器的方法:
Report getData(@RequestBody final Info info){
//Some code here...
}
Here Info is model class which further have Business class with getter and setter methods 这里的Info是模型类,它进一步具有带有getter和setter方法的Business类
When I POST above request at the time of deserialization first block converted into java object but second one gets simply ignored. 当我在反序列化时发布上述请求时,第一个块转换为java对象,但是第二个块被简单地忽略了。 I know this is valid json but couldn't understand why 2nd block gets ignored at the time of deserialization .
我知道这是有效的json,但无法理解为什么在反序列化时第二个块会被忽略。
I tried Deserialization and jackson parser properties but couldn't find any readily available solution. 我尝试了反序列化和杰克逊解析器属性,但找不到任何可用的解决方案。 I am using jackson library and Spring Boot framework.
我正在使用杰克逊库和Spring Boot框架。
The behaviour which I am expecting is that it should throw Bad Request instead of treating it as valid json request.
我期望的行为是它应该抛出Bad Request而不是将其视为有效的json请求。
Suggest an approach to achieve this. 建议一种实现此目的的方法。 Thanks!
谢谢!
You are sendind a list
so it's better to be in below format: 您将收到一份
list
因此最好采用以下格式:
{
"Businesses" : [
{"name": "ABC"},
{"name": "ABC"}
]
}
And in case of recieving a list
, your method in controller should accept a list: 并且在接收
list
情况下,控制器中的方法应接受一个列表:
@POST
@Path("/somePath")
@Consumes({ MediaType.APPLICATION_JSON })
your_method(ArrayList<Business> businesses){
//...your code
}
因此,您的@RequestBody
批注应使用List<Info>
而不是Info
并且在发送数据时也应使用json列表[{"name": "ABC"},{"name": "XYZ"}]
You can POST a JsonArray like that: 您可以像这样发布一个JsonArray:
[
{
"Business": {
"name": "ABC"
}
}
{
"Business": {
"name": "XYZ"
}
}
]
than just loop over the JsonObjects. 不仅仅是在JsonObjects上循环。 You can use Google's GSON or what i personally like to use, the MinimalJson ( https://mvnrepository.com/artifact/com.eclipsesource.minimal-json/minimal-json ) dependency to parse objects and determine if they comply the standard.
您可以使用Google的GSON或我个人喜欢使用的MinimalJson( https://mvnrepository.com/artifact/com.eclipsesource.minimal-json/minimal-json )依赖项来解析对象并确定它们是否符合标准。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.