@PathVariable、@RequestParam、@RequestBody的区别

@PathVariable
url路径请求参数,例如我们访问自己QQ空间时,请求的url是 https://user.qzone.qq.com/1010824413 ,此时@PathVariable 获取的参数就是url中的QQ号码。

代码示例:

@ApiOperation("删除指定用户信息")
@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
@ResponseBody
public CommonResult delete(@PathVariable Long id) {
    int count = adminService.delete(id);
    if (count > 0) {
        return CommonResult.success(count);
    }
    return CommonResult.failed();
}

@RequestParam
接收请求所带的param,例如http://localhost:8080/index?A=a&B=b&C=c,此时@RequestParam获取的参数就是A=a,B=b,C=c

代码示例:

@ApiOperation("根据用户名或姓名分页获取用户列表")
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
public CommonResult> list(@RequestParam(value = "keyword", required = false) String keyword,
                                               @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
    List adminList = adminService.list(keyword, pageSize, pageNum);
    return CommonResult.success(CommonPage.restPage(adminList));
}

@RequestBody
接收json、xml等类型的参数,常见的例如参数是一个封装好的对象

代码示例:

@ApiOperation(value = "用户注册")
@RequestMapping(value = "/register", method = RequestMethod.POST)
@ResponseBody
public CommonResult register(@RequestBody UmsAdminParam umsAdminParam, BindingResult result) {
    UmsAdmin umsAdmin = adminService.register(umsAdminParam);
    if (umsAdmin == null) {
        CommonResult.failed();
    }
    return CommonResult.success(umsAdmin);
}

你可能感兴趣的:(java)