jQuery 使用 Spring MVC 将 JSON 数组转换为 java List<String>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15587179/
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
Converting JSON array into java List<String> with Spring MVC
提问by Gonzalo Garcia Lasurtegui
I have a list of string in my javascript code which I send via JQuery to a REST based service as follows:
我的 javascript 代码中有一个字符串列表,我通过 JQuery 将其发送到基于 REST 的服务,如下所示:
var ids = [];
$("input:checked").each(function() {
ids.push(this.id);
});
var selectedIds = JSON.stringify(ids);
$.post("/todonotes/tasks/removeTask", selectedIds,function(e) {
}, "json");
As you can see, I convert a javascript array into a JSON array.
如您所见,我将 javascript 数组转换为 JSON 数组。
Now, in the server side, I use Spring MVC and Hymanson to receive and parse the input JSON:
现在,在服务器端,我使用 Spring MVC 和 Hymanson 来接收和解析输入的 JSON:
@RequestMapping(value="/tasks/removeTask", method = RequestMethod.POST)
public @ResponseBody String removeTask(@RequestBody List<String> selectedIds) {
...
}
But I always get:
但我总是得到:
HTTP Status 415 -
--------------------------------------------------------------------------------
type Status report message
description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
I tried to remove the quotes from the JSON object, and also tried using @RequestParam
without success.
我试图从 JSON 对象中删除引号,也尝试使用@RequestParam
但没有成功。
Update as per OQJF suggestion:
根据 OQJF 建议更新:
I modified my post request as follows:
我修改了我的帖子请求如下:
$.post("/todonotes/tasks/removeTask", {selectedIds: selectedIds},function(e) {
}, "json");
Now my controller method gets called, but each List
element is populated with the double quotes and brackets. This the List argument:
现在我的控制器方法被调用,但每个List
元素都用双引号和括号填充。这是 List 参数:
[["15", "21"]]
So e.g. the first element looks like:
所以例如第一个元素看起来像:
["15"
I would prefer not to parse each String
element of the List
.
我宁愿不解析每个String
的元素List
。
I also tried removing the JSON.stringify
conversion but my controller didn't even get called. With this I got:
我也尝试删除JSON.stringify
转换,但我的控制器甚至没有被调用。有了这个,我得到了:
HTTP Status 400 - Required List parameter 'selectedIds' is not present
--------------------------------------------------------------------------------
type Status report
message Required List parameter 'selectedIds' is not present
description The request sent by the client was syntactically incorrect.
Update
更新
I tried to create an object to hold my ids, as follows:
我尝试创建一个对象来保存我的 ID,如下所示:
public class TaskIdHolder {
private String[] selectedIds;
public String[] getSelectedIds() {
return selectedIds;
}
public void setSelectedIds(String[] selectedIds) {
this.selectedIds = selectedIds;
}
}
Then I modified my controller method signature:
然后我修改了我的控制器方法签名:
public @ResponseBody String removeTask(@RequestBody TaskIdHolder selectedIds) {
And my JSON request looks like this:
我的 JSON 请求如下所示:
{"selectedIds":["15"]}
I also modified my Jquery code as per Andrei's suggestion:
我还按照 Andrei 的建议修改了我的 Jquery 代码:
var data = JSON.stringify({selectedIds:selectedIds});
$.post("/todonotes/tasks/removeTask", data, function(e) {
Where selectedIds is a JavaScript array.
其中 selectedIds 是一个 JavaScript 数组。
But now I get:
但现在我得到:
POST http://localhost:8080/todonotes/tasks/removeTask 415 (Unsupported Media Type)
采纳答案by Andrei
Try giving the the request parameter an explicit name:
尝试为请求参数指定一个明确的名称:
$.post("/todonotes/tasks/removeTask", {selectedIds: selectedIds},...
Update. You can also try to stringify the whole request data object to JSON:
更新。您还可以尝试将整个请求数据对象字符串化为 JSON:
var data = {selectedIds: selectedIds};
$.post("/todonotes/tasks/removeTask", JSON.stringify(data),...
回答by OQJF
As I know that you don't need to use JSON.stringify(),
your code can be like this:
据我所知,您不需要使用JSON.stringify(),
您的代码可以是这样的:
var ids = [];
$("input:checked").each(function() {
ids.push(this.id);
});
$.post("/todonotes/tasks/removeTask", $.param({'id': ids}, true),function(e) {
}, "json");
And on the server side, you should specify the value of:
在服务器端,您应该指定以下值:
public methodName(@RequestParam("id") List<String> selectedIds)
回答by user2608369
Try the following, no need to change your controller's method signature or create an object to hold your ids.
尝试以下操作,无需更改控制器的方法签名或创建一个对象来保存您的 id。
$.post("/todonotes/tasks/removeTask", {selectedIds: selectedIds.join(',')},function(e) {
}, "json");
That way you'll be sending your ids like selectedIds: id1,id2,id3
which is how the Controller is expecting them to be if you declare your request param to be String[]
or List<String>
(works for both scenarios).
这样selectedIds: id1,id2,id3
,如果您将请求参数声明为String[]
或List<String>
(适用于两种情况),您将发送您的 id,就像控制器期望它们的方式一样。