Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring:将对象列表从Ajax传递到RestController_Ajax_Json_Spring - Fatal编程技术网

Spring:将对象列表从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

我有这个问题。我尝试了很多方法,但都没有解决

我的问题是将对象列表从ajax传递到restcontroller 我的建议是:

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可以解决我的问题!我不知道以前添加时为什么会出错:(