2

I want to send and retrieve HashMap through angularjs and receive it in springmvc controller. I have successfully send and received List but unable to send HashMap. My code is.

$scope.addskill = function(skills){     
//  $scope.list = [];       
//  $scope.list.push(skills.skillName, skills.expMonth, skills.expYear, skills.experties);          
    var map = {};
    map['name'] = skills.skillName;
    map['month'] = skills.expMonth;
    map['year'] = skills.expYear;
    map['experties'] = skills.experties;

    alert(map['name']);
    var response = $http.get('/JobSearch/user/addskill/?map=' +map);
//  var response = $http.get('/JobSearch/user/addskill/?list=' +$scope.list);
    response.success(function(data, status, headers, config){
        $scope.skills = null;
        $timeout($scope.refreshskill,1000);             
    });             
    response.error(function(data, status, headers, config) {
        alert( "Exception details: " + JSON.stringify({data: data}));
    });     
};

My mvc Controller is :

@RequestMapping(value = "/addskill", method = RequestMethod.GET)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void addStudentSkill(@RequestBody HashMap<String,String> map){

    System.out.println(map.get("name"));
/*      
 *      public void addStudentSkill(@RequestParam("list") List list){
    try{    
        StudentSkills skills = new StudentSkills();
        skills.setSkillName(list[0]);
        skills.setExpMonth(Integer.parseInt(list[1]));
        skills.setExpYear(Integer.parseInt(list[2]));
        skills.setExperties(list[3]);
        skills.setStudent(studentService.getStudent(getStudentName()));
        studentService.addStudentSkill(skills);
    }catch(Exception e){};

*/
}

Commented code works when i send and receive List. I want to use key to retrieve data. If there is any better way please suggest.

The error is cannot convert java.lang.string to hashmap

Kharoud
  • 307
  • 1
  • 7
  • 20

1 Answers1

3

You're sending the map as a request parameter. And you're trying to read it in the request body. That can't possibly work. And GET requests don't have a body anyway.

Here's how you should do it:

var parameters = {};
parameters.name = skills.skillName;
parameters.month = skills.expMonth;
parameters.year = skills.expYear;
parameters.experties = skills.experties;

var promise = $http.get('/JobSearch/user/addskill', {
    params: parameters
});

And in the Spring controller:

@RequestMapping(value = "/addskill", method = RequestMethod.GET)
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void addStudentSkill(@RequestParam("name") String name,
                            @RequestParam("name") String month,
                            @RequestParam("name") String year,
                            @RequestParam("name") String experties) {
    ...
}

That said, given the name of the method addStudentSkill, and the fact that it doesn't return anything, it seems this method is not used to get data from the server, but instead to create data on the server. So this method should be mapped to a POST request, and the data should be sent as the body:

var data = {};
data.name = skills.skillName;
data.month = skills.expMonth;
data.year = skills.expYear;
data.experties = skills.experties;

var promise = $http.post('/JobSearch/user/addskill', params);

and in the controller:

@RequestMapping(value = "/addskill", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.CREATED)
public void addStudentSkill(@RequestBody Map<String, String> data) {
    ...
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • need CSRF token when sending post request. also @requestParam HashMap map not works, but String[] list works in get request. – Kharoud Mar 15 '15 at 12:44
  • If CSRF token needs to be sent, then send it... Don't use a GET when a POST should be used. – JB Nizet Mar 15 '15 at 12:50
  • My data can be transferred through get request though little cumbersome but i think i should not use CSRF security token if not absolutely needed. Thank you for answer it was helpful. – Kharoud Mar 15 '15 at 14:44
  • Needs to be @RequestParam("month") String month, etc. Other than that, this worked for me – Greg Dougherty Mar 16 '15 at 19:34