2017-06-26 72 views
1

我試圖通過AJAX調用發送一個POJO數組,每個POJO包含從客戶端到Spring MVC RestController的其他POJO列表。如何將包含對象數組的對象數組傳遞給使用jQuery的Spring MVC控制器?

我有以下POJO也就是提交:

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>(); 

它包含的改變路徑列表:

public class ChangedPath extends PatchFile { 

private char type; 
private String copyPath; 

我有以下春天控制器:

@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調用:

$.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: [email protected]; line: 1, column: 9] 

我在做什麼錯?

+0

這很奇怪,但看看我發現,試試看:''「提交」:「'+ JSON.stringify(提交)+'''''。這個人有類似的問題 –

回答

2

傳遞JSON字符串作爲接受控制器方法的數據。

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

@dannemp在我看來,我們嘗試了這種方法,但你寫它並沒有幫助你o_0 –

相關問題