FJSNorman
FJSNorman

Reputation: 33

Spring @RequestBody: the request sent by the client was syntactically incorrect

Hi everyone this is my first question here, hope you can help me with this issue I'm having right now.

I want to send a JSON Object using JQuery to a Spring Controller. The format of the JSON Object is as follows:

{"ssoArray":["21212", "231341"], "groupArray":["g10002", "g10003"]}

But I got the error message: the request sent by the client was syntactically incorrect

My Environment:

JQuery code: Update: Added the full code of the javascript method that makes the ajax call

function addRemoveAdmin(action, ssoArray, groupArray) {
    var uri = actionPath + "/";
    var jsonParameter = {"ssoArray":ssoArray, "groupArray":groupArray};

    if(action == "add") {
        uri = uri + addAdminAction;
    } else {
        uri = uri + removeAdminAction;
    }               

    console.log(typeof jsonParameter);

    $.ajax({
        url:uri,
        type:"POST",
        data:jsonParameter,
        contentType:"application/json; charset=utf-8",
        dataType:"json",
        success: function(){
            alert(data);
        }
    });
}

Controller code:

@RequestMapping(value = "/addAdmin", method = RequestMethod.POST)
public String addAdmin(@RequestBody final AdminAndGroup personsAndGroups) throws Exception {
    LOGGER.info("[RequestController] - addAdmin start");

    LOGGER.info(personsAndGroups.toString());

    return "OK";
}

Mapping class:

public class AdminAndGroup implements Serializable {

private static final long serialVersionUID = 9024455757311860269L;

private List<String> ssoArray;
private List<String> groupArray;

public AdminAndGroup(){}

public List<String> getSsoArray() {
    return ssoArray;
}
public void setSsoArray(List<String> ssoArray) {
    this.ssoArray = ssoArray;
}
public List<String> getGroupArray() {
    return groupArray;
}
public void setGroupArray(List<String> groupArray) {
    this.groupArray = groupArray;
}

@Override
public String toString() {
    return "AdminAndGroup [ssoArray=" + ssoArray + ", groupArray="
            + groupArray + "]";
}

}

I have used java.util.List to map the arrays that come inside the JSON Object but I'm not sure if this is the right way to tackle the issue.

Also I have searched in Stack Overflow and I haven't found an example on how to map arrays inside a JSON Object.

Thanks in advance.

Upvotes: 3

Views: 7184

Answers (1)

Kevin B
Kevin B

Reputation: 95022

If you want to send json, you must convert your object to json. Otherwise, jQuery will convert it to a param string

data: JSON.stringify(jsonParameter)

Upvotes: 3

Related Questions