I'm trying to post JSON data to Spring Portlet Controller method. The Modelattribute object is nested. Here is the JSON:
$(document).on({
change: function() {
...
...
formData = {
"name": "Demo",
"id": 724,
"periodId": 2015,
"orgId": 4,
"fieldGroupList": [{
"name": "instructions",
"label": "Instructions",
"fieldList": [{
"name": "INSTRUCTION",
"instructionList": [{
"instructionText": "All enabled fields are required for completion of this screen."
}],
"type": "INSTRUCTION"
}]
}]
};
Ajax:
$.ajax({
async: "false",
global: "false",
url: validateURL,
type: "POST",
data: formData,
dataType: "json"
}).done(function(json){
console.log(json);
}).fail(function(jqXHR, textStatus, error) {
console.log("responseText: "+jqXHR.responseText);
});
Controller:
@ResourceMapping(value = "validateURL")
public void validate(@ModelAttribute(value = "formData") Measure measure,
BindingResult bindingResult, ResourceRequest request, ResourceResponse response, ModelMap model) throws Exception {
System.out.println("ab:::"+measure.getId());
}
Model:
public class Measure
{
private String name;
private List<MeasureFieldGroup> fieldGroupList = new ArrayList<MeasureFieldGroup>();
...
}
Also everything works fine if the JSON is changed to:
formData = {
"name": "Demo",
"id": 724,
"periodId": 2015,
"orgId": 4
};
Error in controller:
org.springframework.beans.InvalidPropertyException: Invalid property 'fieldGroupList[0][fieldList][0][instructionList][0][instructionText]' of bean class abc.measures.base.Measure]: Illegal attempt to get property 'fieldGroupList' threw exception; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'fieldGroupList[0][fieldList][0][instructionList][0][instructionText]' of bean class [abc.measures.base.Measure]: Property referenced in indexed property path 'fieldGroupList[0][fieldList][0][instructionList][0][instructionText]' is neither an array nor a List nor a Set nor a Map; returned value was [abc.measures.base.MeasureFieldGroup@72fd67c]
My issue is very similar to Post Nested Object to Spring MVC controller using JSON and Spring Portlet Jquery Ajax post to Controller
but can't use ResponseBody and RequestBody due to Spring Portlet. Any help would be appreciated a lot.
Thanks