Issue
I have holding object as follows:
public class Transfer implements Serializable {
private Integer transferId;
private Integer transferTypeId;
private String storeId;
private String userId;
private Integer titleId;
private Integer statusId;
private String inWorkerId;
private String outWorkerId;
private Date createDt;
private Date updateDt;
// getters & setts
}
And I have var reqRow
that need to be sent to the controller
function onClickSave(){
var rows = $('#transferEditGrid').jqGrid('getRowData');
var reqRow = [];
for (i = 0; i < rows.length; i++)
{
var rowObj = {};
rowObj.storeId = rows[i].storeId;
rowObj.inWorkerId = rows[i].inWorkerId;
rowObj.outWorkerId = rows[i].outWorkerId;
rowObj.transferTypeId = rows[i].transferTypeId;
rowObj.statusId = rows[i].statusId;
rowObj.storeId = rows[i].storeId;
reqRow.push(rowObj);
}
//send reqRow to the Controller
$.ajax({
type:'POST',
url:'${contextPath}/resource-transfer/update.do',
dataType:"json",
data : {rows : reqRow},
//data:JSON.stringify(reqRow),
success:function(response){
alert("success");
}
});
}
The controller is following :
@RequestMapping(value = "/update", method = { RequestMethod.GET, RequestMethod.POST }, produces = "application/json; charset=utf-8")
@ResponseBody
public String transferUpdate(@RequestBody List<Transfer> rows) throws JSONException, InterruptedException {
System.out.println("in transfer update section");
return null;
}
Why I could not pass the array object to the controller? Did I misunderstand the usage of the Ajax call?
Thank you
Solution
First, wrap List to another java object
public class TransferDTO { private List<Transfer> rows; // getter & setter }
Use this inplace of
List<Transfer>
in your endpoint.
public String transferUpdate(@RequestBody TransferDTO data)
- Specify
contentType
in your AJAX post
contentType: 'application/json'
- Manually stringify the data
data : JSON.stringify({rows: reqRow})
Answered By - Bnrdo