Spring:将对象列表从Ajax传递到RestController
我有这个问题。我尝试了很多方法,但都没有解决 我的问题是将对象列表从ajax传递到restcontroller 我的建议是:Spring:将对象列表从Ajax传递到RestController,ajax,json,spring,Ajax,Json,Spring,我有这个问题。我尝试了很多方法,但都没有解决 我的问题是将对象列表从ajax传递到restcontroller 我的建议是: var answers = [{"question_id":"John", "answer":"Doe"},{"question_id":"Anna", "answer":"Smith"}]; $("#btn-submit").click(function(){ $.ajax({ type : "PO
var answers = [{"question_id":"John", "answer":"Doe"},{"question_id":"Anna", "answer":"Smith"}];
$("#btn-submit").click(function(){
$.ajax({
type : "POST",
url : "check",
dataType : 'json',
data: answers,
success : function() {
console.log("ok");
},
error : function(errors_dict) {
console.log("error");
}
});
return false;
});
我的Spring服务器我尝试了ArrayList或Answers:答案的类列表
@RequestMapping(value = "/check", method = RequestMethod.POST)
public @ResponseBody String answer(Answers answers) {
...
}
但是答案是。size()总是0我不是大师,但我会想到一些事情。您能自动将
JSON
转换为String
吗。我通常将JSON转换为HashMap
。另外,如果您将`consumes=“application/json”`添加到@RequestMapping
,它会改变什么吗?@bitoiu感谢您的帮助!我发现将@RequestBody添加到@ResponseBody可以解决我的问题!我不知道以前添加时为什么会出错:(