java 将 JSON 数组发布到 Spring Boot RestController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43997857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Post a JSON Array into Spring Boot RestController
提问by orrymr
I am posting something like this:
我正在发布这样的内容:
{
"stuff": [
{
"thingId": 1,
"thingNumber": "abc",
"countryCode": "ZA"
},
{
"thingId": 2,
"thingNumber": "xyz",
"countryCode": "US"
}
]
}
I can retrieve the data in the controller as follows:
我可以按如下方式检索控制器中的数据:
@RequestMapping(value = "/stuffs", method = RequestMethod.POST)
public String invoices(@RequestBody Map<String, List <Stuff>> stuffs) {
return "Hooray";
}
What I'd really like to do is to only pass in a List of Stuff into the controller; ie:
我真正想做的是只将一个 List of Stuff 传入控制器;IE:
@RequestMapping(value = "/stuffs", method = RequestMethod.POST)
public String invoices(@RequestBody List <Stuff> stuffs) {
return "I'm sad that this won't work.";
}
However, Hymanson does not like this. The error I get is the following:
然而,Hyman逊并不喜欢这样。我得到的错误如下:
Could not read JSON document: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@2a30a2f4; line: 1, column: 1]; nested exception is com.fasterxml.Hymanson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@2a30a2f4; line: 1, column: 1]
无法读取 JSON 文档:无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例\n 在 [来源:java.io.PushbackInputStream@2a30a2f4; 行:1,列:1];嵌套异常是 com.fasterxml.Hymanson.databind.JsonMappingException:无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例\n 在 [来源:java.io.PushbackInputStream@2a30a2f4; 行:1,列:1]
I cannot send in the following (since it's not valid JSON):
我无法发送以下内容(因为它不是有效的 JSON):
{
[
{
"thingId": 1,
"thingNumber": "abc",
"countryCode": "ZA"
},
{
"thingId": 2,
"thingNumber": "xyz",
"countryCode": "US"
}
]
}
I am sure that this is something simple, but I cannot distil an answer from my StackOverflow and Google searches.
我确信这很简单,但我无法从我的 StackOverflow 和 Google 搜索中提取答案。
Any help would be appreciated! :)
任何帮助,将不胜感激!:)
回答by Amer Qarabsa
you only need to send this without the curly brackets
你只需要在没有大括号的情况下发送这个
[
{
"thingId": 1,
"thingNumber": "abc",
"countryCode": "ZA"
},
{
"thingId": 2,
"thingNumber": "xyz",
"countryCode": "US"
}
]
In your controller when you tell Hymanson that it should be parsing a list it should be a list not an object containing a list
在你的控制器中,当你告诉Hyman逊它应该解析一个列表时,它应该是一个列表而不是一个包含列表的对象
回答by S. Pan
Note that if you're trying to get an array of JSON without a Java Class (the Class being "Stuff" in the OP's question) you will need to set up the Spring Boot Controller like this:
请注意,如果您尝试获取没有 Java 类的 JSON 数组(OP 问题中的类是“Stuff”),您将需要像这样设置 Spring Boot 控制器:
@RequestMapping(value = "/stuffs", method = RequestMethod.POST)
public String invoices(@RequestBody Map<String, Object> [] stuffs) {
return "Hooray";
}