Java 如何将整数数组传递给弹簧控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19440456/
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
How do I pass an array of integer into spring controller?
提问by user4127
script Array
of int
and I wish to pass into Spring Controller
. but I keep getting
的脚本Array
,int
我希望传入Spring Controller
. 但我不断得到
400 bad request.
if my js array
is
如果我js array
是
array = [1,2,3,4]
array -> 400 bad request
JSON.Stringify(array) -> I will get [1,2,3,4]
$.ajax({//jquery ajax
data:{"images": array},
dataType:'json',
type:"post",
url:"hellomotto"
....
})
when I loop the string List
.. the first element will be '[1'
当我循环string List
.. 第一个元素将是'[1'
@RequestMapping(value = "/hellomotto", method = Request.POST)
public void hellomotto(@RequestParam("images") List<String> images){
sysout(images); -> I will get [1,2,3,4]
}
public void
公共无效
May I know how can I do this properly? I have tried different combination
我可以知道如何正确执行此操作吗?我尝试了不同的组合
采纳答案by mapm
The following is a working example:
以下是一个工作示例:
Javascript:
Javascript:
$('#btn_confirm').click(function (e) {
e.preventDefault(); // do not submit the form
// prepare the array
var data = table.rows('.selected').data();
var ids = [];
for(var i = 0; i < data.length; i++) {
ids.push(Number(data[i][0]));
}
$.ajax({
type: "POST",
url: "?confirm",
data: JSON.stringify(ids),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){
alert(data);
},
failure: function(errMsg) {
alert(errMsg);
}
});
});
Controller:
控制器:
@RequestMapping(params = "confirm", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody int confirm(@RequestBody Long[] ids) {
// code to handle request
return ids.length;
}
回答by Viktor K.
@RequestParam is used to bind request parameters, so if you do something like
@RequestParam 用于绑定请求参数,所以如果你做类似的事情
@RequestMapping(value = "/hellomotto", method = Request.POST)
public void hellomotto(@RequestParam("image") String image){
...
}
and you do post to /hellomotto?image=test, the image variable in hellomotto method will contain "test"
并且您发布到 /hellomotto?image=test,hellomotto 方法中的图像变量将包含“test”
What you want to do is to parse Request body , so you should you should use @RequestBody annotation :
你想要做的是解析 Request body ,所以你应该使用 @RequestBody 注释:
http://docs.spring.io/spring/docs/3.0.x/reference/mvc.html#mvc-ann-requestbody
http://docs.spring.io/spring/docs/3.0.x/reference/mvc.html#mvc-ann-requestbody
it is using Hymanson labrary (so you will have to include it as your dependency) to parse json object into java object.
它使用 Hymanson labrary(因此您必须将其作为依赖项包含在内)将 json 对象解析为 java 对象。
回答by Oomph Fortuity
I think you wantAjax call,through ajax, you are sending List of Integers so in spring your controller will be
我想你想要 Ajax 调用,通过 ajax,你正在发送整数列表,所以在春天你的控制器将是
@RequestMapping(value = "/hellomotto", method = Request.POST)
@ResponseBody
public void hellomotto(@RequestParam("images") List<Integer> images){
sysout(images); -> I will get [1,2,3,4]
}
*@ResponseBodyis missing in Your Code
* @ResponseBody在您的代码中丢失