1

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?

1
  • that's strange but look what I've found, try it: ' "commits" : " '+JSON.stringify(commits)+' " '. the guy had the similar issue Commented Jun 26, 2017 at 9:22

1 Answer 1

3

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);
    }
})
Sign up to request clarification or add additional context in comments.

1 Comment

@dannemp it seems to me that we tried this approach but you wrote it hadn't helped you o_0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.