sending HashMap by angularjs $http.get in spring mvc

≯℡__Kan透↙ 提交于 2020-01-24 19:19:01

问题


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:


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) {
    ...
}


来源:https://stackoverflow.com/questions/29060493/sending-hashmap-by-angularjs-http-get-in-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!