This is my solution:
js class that we want to send:
function Event(id, name) {
this.starttime = '';
this.endtime = '';
this.shiftUsers = [];
this.name = name
this.id = id;
}
After that fill array:
var shifts = [];
shifts.push(new Event(newid, current_name));
Ajax part:
$(document).on('click', '#test1', function(e) {
console.log('Test ajax save');
$.ajax({
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
type: "POST",
url: "ajaxCreateShiftKind2.htm",
dataType: "json",
data: JSON.stringify(shifts),
contentType: 'application/json',
success: function(html) {
alert(html);
}
});
});
And java part, recive:
@RequestMapping(value = "/ajaxCreateShiftKind2", method = RequestMethod.POST)
public @ResponseBody
Map<String, Object> ajaxCreateShiftKind2(HttpServletRequest request) {
Map<String, Object> myModel = new HashMap<String, Object>();
//org.codehaus.jackson.map.ObjectMapper
ObjectMapper om = new ObjectMapper();
//Input input = mapper.readValue( request.getInputStream() , Class Input... );
Event[] se;
try {
se = om.readValue(request.getInputStream(), Event[].class);
} catch (Exception e) {
se = null;
}
myModel.put("ajaxResult", 1);
myModel.put("ajaxMessage", "Added succesful");
myModel.put("ajaxShiftKind", "Hello world!");
myModel.put("ajaxData", se);
return myModel;
}
And class that we expect from js, setters and getters are necessary:
public class Event {
int id;
String name, endtime, starttime;
int[] shiftUsers;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStarttime() {
return starttime;
}
public void setStarttime(String Starttime) {
this.starttime = Starttime;
}
public String getEndtime() {
return endtime;
}
public void setEndtime(String Endtime) {
this.endtime = Endtime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setShiftUsers(int[] shiftUsers) {
this.shiftUsers = shiftUsers;
}
public int[] getShiftUsers() {
return this.shiftUsers;
}
}