1. 程式人生 > >Could not read JSON: Can not deserialize instance of java.lang.Integer out of START_OBJECT token

Could not read JSON: Can not deserialize instance of java.lang.Integer out of START_OBJECT token

Controller中的程式碼如下:

       @RequestMapping(value="/delete",method=RequestMethod.POST,produces="application/json")

@ResponseStatus(HttpStatus.OK)
public void delete(@RequestBody Integer id){
this.moduleService.deleteModule(id);

}

ajax請求:

//刪除
$(".delete_line").live('click',function(){
//url:'/uc/module/delete/',
var del=$(this);
var id=$(this).attr('alt');
if(confirm('確定要刪除嗎?')){
var params = { id : id};
var str =Ab.encode(params);
alert(str);
$.ajax({
type: "post",
data: str,
dataType:"json",
//url:G_URL['module/create'],
url: G_ROOT+'/module/delete',
contentType:'application/json;charset=UTF-8',
success:function(data){
getTree('right');
}
});
}
return false;
});

Spring 會將{id:id}這個json轉換成Map物件,只要將@requestBody中的引數改成Map就可以了,如下

@RequestMapping(value="/delete",method=RequestMethod.POST,produces="application/json")
@ResponseStatus(HttpStatus.OK)
public void delete(@RequestBody Map map){
this.moduleService.deleteModule(Integer.parseInt(map.get("id")+""));
}