简体   繁体   中英

pass complex data from angularjs controller to Spring MVC controller

I have a form with following sections ( following example is for understanding purpose)

GeneralInformation - it 's an object with Cityname (String) and population(int)

Location Information: It's an object with locationCode (int) and Nearest HospitalName(String)

Companies: It's an object with company details. There is list of companies dynamically added with Company as object. Basically List

Hospitals: it's like List

// generalInfo - populated from form

//locationInfo - populated from form

//companiesArr[] // this is dynamicallypopulated (each row each object) companies array

// hospitalsArr[] // // this is dynamicallypopulated (each row each object) Hospitals array

//Angular code starts.. controller('addGeneralController', function($scope, close,Service) {

        $scope.companiesArr = [];
        $scope.comapnyName='';
        $scope.companyType='';


        $scope.hospitalsArr = [];
        $scope.hospitalName='';
        $scope.locationCode='';



        $scope.generalInfo = {};
        $scope.locationInfo = {};
        $scope.companies = {};
        $scope.hospitals = {};

        $scope.dataInfo = {};//this is to carry entire objects and arrays

       //Following method calls after populating data from form and submit.
       //companiesArr,hospitalsArr are populated from front end and passing as submission parameters

        $scope.saveGeneral = functio(generalInfo,locationInfo,companiesArr,hospitalsArr){ 

            $scope.companies = companiesArr;

            $scope.hospitals = hospitalsArr;

            //Create an empty array
            //$scope.dataInfo = [];

            $scope.dataInfo.push({'generalInfo' : generalInfo, 'locationInfo' : locationInfo,'companies' : $scope.companies,'hospitals' : $scope.hospitals});

    $http.post("/addGeneralData",$scope.dataInfo);


        });

//Angular code ends..

     It's not reaching to the following Spring MVC method:

    @RequestMapping(value = "/addGeneralData", method = RequestMethod.POST)
        public @ResponseBody String addGeneralData(@RequestBody List<Data> dataInfo){

           // not reaching here.With simple parametrs it's reaching here, so no other mapping issue apart from this complex data
          // Data - is an object with  generalInfo as object,locationInfo as object,                //companies List ,hospitals List as it's attributes.

        Data data  = dataInfo.get(0);
            GeneralInfo generalInfo = data.getgeneralInfo();
            LocationInfo locationInfo = data.getLocationInfo();
            List<Company> companies = data.getCompanies();
            List<Hospital> hospitals = data.getHospitals();


    }

Basically I want to know how can I transfer this complex data from angular controller to Spring MVC controller?

Please share the request sent from the browser to comment more

It certainly looks like you are sending DataInfo Object but receiving List dataInfo in your controller. there is a mismatch.

Change the Signature of the handler method

to public @ResponseBody String addGeneralData(@RequestBody DataInfo dataInfo)

Do you have any exception ?
It is very likely that you get Serialization Exception due to List interface passed as parameter to controller. Spring just can not initialize new instance of List . Try to use array instead of List.
For example

 @RequestMapping(value = "/addGeneralData", method = RequestMethod.POST)
    public @ResponseBody String addGeneralData(@RequestBody Data[] dataInfo){
        Data data  = dataInfo[0];
        GeneralInfo generalInfo = data.getgeneralInfo();
        LocationInfo locationInfo = data.getLocationInfo();
        Company[] companies = data.getCompanies();
        Hospital[] hospitals = data.getHospitals();


}

Be sure that you use concrete implementations, not interfaces in your Data object. Hope it helps

Thanks for your response.It's worked when I changed to array instead of list. I have changed all lists inside the Data objects also to array. In addition to that make sure that all data passing from input is as per the type mentioned in the concrete object. For example any data mentioned as int , make sure it's passing int only. If it is complex form and before input validation we are integrating front end with backend, make sure all data we passed exactly as the type mentioned in the mapping object.Is it good practice to use array as parameter in MVC controller?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM