如何使用jQuery将包含对象数组的对象数组传递给Spring MVC控制器? [英] How to pass array of objects containing arrays of objects to Spring MVC controller using jQuery?

查看:227
本文介绍了如何使用jQuery将包含对象数组的对象数组传递给Spring MVC控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过AJAX调用将POJO数组从客户端发送到Spring MVC RestController,每个POJO包含一个其他POJO列表.

I am trying to send an array of POJOs each containing a list of other POJOs from the client side to a Spring MVC RestController through an AJAX call.

我有以下POJO,即Commit:

I have the following POJO that is the Commit:

public class Commit {

private long revision;
private Date date;
private String author;
private String comment;
private String akuiteo;

private List<ChangedPath> changedPathList = new ArrayList<ChangedPath>();

它包含已更改路径的列表:

It contains a List of changed paths:

public class ChangedPath extends PatchFile {

private char type;
private String copyPath;

我有以下Spring控制器:

I have the following Spring controller:

@RestController
public class AkuiteoMapController {

static Logger log = Logger.getLogger(PatchDemoApplication.class.getName());

public AkuiteoMapController() {
    // TODO Auto-generated constructor stub
}

@RequestMapping(value="/akuiteoMap")
@ResponseBody
public AkuiteoMap getAllCommits(@RequestBody Commit[] commits) throws IOException{
    log.info("inside akuiteoMap");
    AkuiteoMap akuiteoMap=new AkuiteoMap();
    akuiteoMap= UserService.getAkuiteoMap(commits);
    log.info("akuiteo map: "+akuiteoMap);
    return akuiteoMap;
}

}

在客户端,我尝试执行以下ajax调用:

On the client side I try to do the following ajax call:

$.ajax({
        url: 'akuiteoMap',
        method: 'POST',
        dataType: 'json',
        contentType: 'application/json',// charset=utf-8',
        data:{
            commits:JSON.stringify(commits),
            //commits:commits
        },
        success: function(data){
            console.log(data);
        }
    })

我收到以下错误:

2017-06-26 10:58:40.764  WARN 4788 --- [nio-8080-exec-8] 
.w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: 
org.springframework.http.converter.HttpMessageNotReadableException: 
JSON parse error: Unrecognized token 'commits': was expecting ('true', 
'false' or 'null'); nested exception is 
com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'commits': 
was expecting ('true', 'false' or 'null')
at [Source: java.io.PushbackInputStream@e57cb2a; line: 1, column: 9]

我在做什么错了?

推荐答案

传递JSON字符串作为将接受控制器方法的数据.

Pass the JSON string as the data which would accept the controller method.

$.ajax({
    url: 'akuiteoMap',
    method: 'POST',
    dataType: 'json',
    contentType: 'application/json',,
    data : JSON.stringify(commits),
    // ----^^^^^^^^^^^^^^^^^^^^^^^----
    success: function(data){
        console.log(data);
    }
})

这篇关于如何使用jQuery将包含对象数组的对象数组传递给Spring MVC控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆