0

I am trying to pass multiple list from AngularJS to Spring controller through a POST call. If I send just one list as mentioned below it works fine on the Spring controller but would like to know the best way to send multiple list from Angular to Spring.

$scope.formData = [];
var AList = [];
AList.push({'a': 1, 'b' :2});
$scope.formData = AList;
$http.post('saveData', $scope.formData).success(function(resp){});

When I try to send multiple list through the same approach but by using push, it is received in the Spring controller as shown below which I think is valid

$scope.formData.push(Alist);
$scope.formData.push(Blist);

I get something like below in Spring Controller.

[[ {a=1, b=2}, {a=3, b=4} ]]

How do I iterate this in Spring Controller and store it to my domain object.

Is this the correct approach or Is there any better ways to do it

3
  • This is messed up. What do you try to send? A list of what? Your Alist or Blist are not lists (in a meaning of arrays) but a key-value map (or simpler: an object). Do you want to send a list of objects? What your domain object looks like? Commented Dec 28, 2016 at 21:18
  • Thats correct, I want to send list of domain objects. We can assume that the domain object contains the fields a and b. What I did is I received it as simple array of objects @RequestBody Object[] objDetail Then I iterated it using JSONArray and I was able to get the values as desired Commented Dec 29, 2016 at 1:57
  • Ok, now I see. I've posted my answer. Commented Dec 29, 2016 at 10:57

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Sure, I will try this and post my response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.