Reputation: 1019
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.
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;
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;
}
}
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);
}
})
I get the following error:
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]
What am I doing wrong?
Upvotes: 1
Views: 1106
Reputation: 115222
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);
}
})
Upvotes: 3