sending HashMap by angularjs $http.get in spring m

2019-07-19 08:25发布

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

1条回答
倾城 Initia
2楼-- · 2019-07-19 09:25

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) {
    ...
}
查看更多
登录 后发表回答