您的位置:首页 > 理论基础 > 计算机网络

HTTP Status 400 - Required long parameter 'id' is not present

2017-12-20 22:06 2066 查看
在使用SpringMVC的注解@RequestParam(映射请求参数)获取前端传递过来的参数时,报出异常:Required long parameter ‘id’ is not present。

@RequestParam有三个属性,分别如下:

(1) value 请求参数的参数名,作为参数映射名称;

(2) required 该参数是否必填,默认为true(必填),当设置成必填时,如果没有传入参数,报错;

(3) defaultValue 设置请求参数的默认值;

错误代码:
public List<EasyUITreeNode> queryItemCatList(@RequestParam(value="id") long parentId) throws Exception{
List<EasyUITreeNode> resultList = itemCatService.queryItemCatList(parentId);
return resultList;
}
错误分析:这里对前端传入参数指定参数名为id,此时required=true,第一次请求从前端传入的id却没有值,需要给个默认值defaultValue="0"。

正确代码:
public List<EasyUITreeNode> queryItemCatList(@RequestParam(value="id",defaultValue="0") long parentId) throws Exception{
List<EasyUITreeNode> resultList = itemCatService.queryItemCatList(parentId);
return resultList;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: