Your frontend approach is correct. All you need to do is create an empty array, push there as many objects as you need and post it to the server.
//Create an empty array
$scope.formData = [];
//Push as many objects as you need
$scope.formData.push({'a' : 1, 'b' : 2});
$scope.formData.push({'a' : 3, 'b' : 4});
//Post it to server
$http.post('saveData', $scope.formData).success(function(resp){
//handle response
});
But your Spring side can be improved. Arrays of Objects (Object[]) are generally deprecated because they are not type safe and thus are error prone. They should be replaced with parametrized collections (from Java Collections Framework) whenever possible.
In your case you could apply following steps:
Create class of your domain object or DTO, that corresponds to received json objects.
public class MyDomainObject {
private Integer a;
private Integer b;
public MyDomainObject(){ }
//getters and setters omitted
}
Then in your endpoint method switch @RequestBody type from Object[] to List<MyDomainObject>.
@RequestMapping(path = "/saveDate", method = RequestMethod.POST)
public void postDomainObjects(@RequestBody List<MyDomainObject> myDomainObjects){
//Do some business logic.
//i.e. pass further to service or save to database via repository
}
And you receive a list of objects that are exact java representation of json objects from angular. You could iterate the list in a standard java ways, for example using foreach operator:
for (MyDomainObject obj : myDomainObjects) {
//do something with each object.
//i.e. print value of "a" field
System.out.println("Value of a: " + obj.getA());
}
or using streams in case of java 8:
//Apply some filtering, mapping or sorting to your collection.
//i.e. count sum of "a" field values.
int sumOfAFields = myDomainObjects.stream()
.mapToInt(o -> o.getA())
.sum();
NOTE:
Above solution will work if you have configured object mapper. If you use Spring boot with any of web starters you'll get it for free. In standard spring project (with configuration on your own) you must include jackson library to your project's classpath and register ObjectMapper bean in configuration class or xml.