1、@PathVariable
获取路径参数,RUL格式:url/{id},示例:http://localhost:9100/admin/privilege/find/3
Java代码示例:
@GetMapping("/find/{id}")
@ApiOperation(value = "查询权限", notes = " ")
@ApiImplicitParams(
@ApiImplicitParam(paramType = "path", name = "id", value = "权限ID", required = true)
)
public Object findPrivilege(@PathVariable(name = "id") int id) {
Map<String,Object> map = new HashMap<>();
map.put("privilege", privilegeService.findPrivilege(id));
return ApiResponse.success(map);
}
2、@RequestParam
获取查询参数,RUL格式:url?name=张三丰,示例:http://localhost:9100/admin/role/search?name=张三丰
Java代码示例:
@PostMapping("/search")
@ApiOperation(value = "搜索权限", notes = " 支持分页,并提供按权限名称、权限URI、权限码查询功能")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "searchKey", value = "查询关键词,支持:权限名称/权限URI/权限码三类关键词"),
@ApiImplicitParam(paramType = "query", name = "pageNum", value = "分页页码")
})
public Object searchPrivilege(@RequestParam(name = "searchKey", defaultValue = "") String searchKey, @RequestParam(name = "pageNum", defaultValue = "1") int pageNum) {
Map<String,Object> map = new HashMap<>();
map.put("privileges", privilegeService.searchPrivilege(searchKey, pageNum, Tools.pageSize));
return ApiResponse.success(map);
}
3、@RequestBody
获取请求体中的参数,RUL格式:不限,示例:如下图的postman接口测试工具
Java代码示例:
@PutMapping("/update/{id}")
@ApiOperation(value = "更新权限", notes = " ")
public Object updatePrivilege(@RequestBody Privilege privilege) {
// 检查权限ID
if (0>=privilege.getId()) {
return ApiResponse.error(10010, "参数错误");
}
// 处理默认值
if (0==privilege.getSort()) {
privilege.setSort(null);
}
if (0==privilege.getCreateTime()) {
privilege.setCreateTime(null);
}
if (0==privilege.getUpdateTime()) {
privilege.setUpdateTime(Tools.getSystemTimeStamp());
}
int res = privilegeService.editPrivilege(privilege);
if (res>0) {
Map<String,Object> map = new HashMap<>();
map.put("result", res);
return ApiResponse.success(map);
} else {
return ApiResponse.error();
}
}
或者使用Map<String,String>作为参数来接收:
@PutMapping("/update/{id}")
@ApiOperation(value = "更新权限", notes = " ")
public Object updatePrivilege(@RequestBody Map<String, String> privilege) {
// todo
// ...
}
4、@RequestHeader
获取请求头中的参数,URL格式:不限,示例:如下图的postman接口测试工具
Java代码示例:
@GetMapping("/refresh_access_token")
@ApiOperation(value = "刷新接口全局调用凭据", notes = " ")
@ApiImplicitParams(
@ApiImplicitParam(paramType = "header", name = "access_token", value = "接口全局调用凭据", required = true)
)
public Object refreshAccessToken(@RequestHeader(name = "access_token") String access_token) {
Map<String, Object> map = new HashMap();
map.put("access_token", access_token);
return ApiResponse.success(map);
}
5、@CookieValue
Java代码示例:
@GetMapping("/admin/privilege/get_list")
public void demo3(@CookieValue(name = "myCookie") String myCookie) {
System.out.println("myCookie=" + myCookie);
}
// 或者这种方式
@GetMapping("/admin/privilege/get_list2")
public void demo3(HttpServletRequest request) {
for (Cookie cookie : request.getCookies()) {
if ("myCookie".equals(cookie.getName())) {
System.out.println(cookie.getValue());
}
}
}
6、无注解参数绑定
Java代码示例:
@PostMapping(path = "/admin/privilege/save")
public void demo2(Privilege privilege) {
System.out.println(privilege.toString());
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/10490.html