0

I have to pass two string data from angularjs controller to spring controller. Is there any other way of doing this except json? I don't know how to accept json object in spring controller.

controller.js

$scope.cloneRelease = function(release,newReleaseName){
            var dataObj = {
            oldReleaseName : $scope.release,
            newReleaseName : $scope.newReleaseName,
        };
        console.log('inside clone release'+dataObj);
        $http.post('cloneReleaseController/cloneRelease',dataObj).success(function (data) {   
        }); 
        }

Spring controller

public void cloneRelease(String oldReleaseName ,String newReleaseName, @RequestBody ReleaseDAO releaseDAO){
        System.out.println("inside clone controller"+oldReleaseName+" "+newReleaseName);
        ArrayList<TaskDAO> tasks;
        ArrayList<TaskDAO> task = new ArrayList<TaskDAO>();
        if(mongoService.createReleaseService(releaseDAO) != null){
            String releaseIdOfNewRelease = releaseDAO.getId();
            String releaseNameOfNewRelease = releaseDAO.getName();
            tasks = mongoService.fetchTaskForReleaseService(oldReleaseName);
                for(TaskDAO fetchSingleTask : tasks){
                    fetchSingleTask.getRelease().setId(releaseIdOfNewRelease);
                    fetchSingleTask.getRelease().setName(releaseIdOfNewRelease);
                }
                mongoService.addTask(tasks, null);
        }

    }
code98355
  • 21
  • 1
  • 6
  • Look at [passing JSON data to a Spring MVC controller](http://stackoverflow.com/questions/18524524/passing-json-data-to-a-spring-mvc-controller). Your angular code looks ok, but your spring controller needs to looks something like this: `public void cloneRelease(@RequestBody SomePojo dataObj)` or if you're too lazy to define a POJO then `public void cloneRelease(@RequestBody Map dataObj)` – logee Sep 03 '15 at 07:18

2 Answers2

0

In your controller.js inside $scope.cloneRelease you have json object dataObj in client side, simply put the below code you can get in server side with HttpServletRequest as

Client Side:

 $http({method: 'POST', url: 'cloneReleaseController/cloneRelease', params:{str:dataObj}}).
                    success(function(data, status, headers, config) {
                        console.log("data.token " + data.token);
                            }).
                    error(function(data, status, headers, config) {
                        console.log("data.token " + data.token);
                        });

Server Side: String json = request.getParameter("str");

Pradeep
  • 105
  • 6
0

Without a little more of the code I can't be entirely sure what is going on, but two things that I notice based on what you have shown are: While not strictly necessary, you should declare that the controller method accepts JSON data:

@RequestMapping(consumes=MediaType.APPLICATION_JSON_VALUE)

Also your java object needs to match the incoming JSON object, or be annotated to ignore fields that you don't intend to send to the controller. You are passing in a JSON object with attributes oldReleaseName and newReleaseName but on the java side I can't tell what your ReleaseDao class is doing. You may need to annotate it with @JsonIgnoreProperties(ignoreUnknown = true) on the class so it doesn't freak out over missing attributes.

As far as the method signature in the controller goes, I'm not sure that one would be called since you don't tell it where the string parameters come from and they aren't objects that would normally be automatically injected (like the request or model). If you trim down the signature to just the below you should have better luck.

public void cloneRelease(@RequestBody ReleaseDAO releaseDAO)
tokkov
  • 2,959
  • 1
  • 13
  • 9