@PathVariable、@RequestParam、@RequestPart、@RequestBody区别

@PathVariable

  • GET请求中占位符常用,可以有多个
  • @PathVariable 映射 URL 绑定的占位符
  • 通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过
  • @PathVariable(“xxx”) 绑定到操作方法的入参中。

一般与@RequestMapping(method = RequestMethod.GET)一起使用也就是@GetMapping使用

@GetMapping("/getUserByName/{name}")
public User getUser(@PathVariable("name") String name){
   return userService.selectUser(name);
}

请求的url:127.0.0.1:8099/getUser/xiaoming

1、若方法参数名称和需要绑定的url中变量名称一致时,可以简写:

@GetMapping("/getUserByName/{name}")
public User getUser(@PathVariable String name){
    return userService.selectUser(name);
}

2、若方法参数名称和需要绑定的url中变量名称不一致时,写成:

@GetMapping("/getUserByName/{name}")
public User getUser(@PathVariable("name") String userName){
    return userService.selectUser(userName);
}

@RequestParam

  • @RequestParam用来处理 Content-Type 为 application/x-www-form-urlencoded 编码的内容,Content-Type默认为该属性。@RequestParam也可用于其它类型的请求,例如:POST、DELETE等请求。
  • 注解@RequestParam接收的参数是来自HTTP请求体或请求url的QueryString中。
  • RequestParam可以接受简单类型的属性,也可以接受对象类型。
  • 可以有多个

@RequestParam有三个配置参数:

  • required 表示是否必须,默认为 true,必须。
  • defaultValue 可设置请求参数的默认值。
  • value 为接收url的参数名(相当于key值)。
  • 注意:这里如果设置了value的话,前端传参的参数名也必须一样,否则拿不到值。

1、请求的url:127.0.0.1:8099/getUser?userName=xiaoming

@GetMapping("/getUserByName")
public User getUser(@RequestParam("name") String userName){
   return userService.selectUser(userName);
}

2、请求url:127.0.0.1:8099/getUser?userName=xiaoming&pageNum=1&pageSize=10

@GetMapping("/queryUserListByName")
public RespEntity queryUserListByName(@RequestParam("userName") Long userName,
                                             @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                             @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize){
     PageHelper.startPage(pageNum,pageSize);
     List<User> list= UserService.queryUserListByName(userName);
     PageInfo<User> pageInfo = new PageInfo<>(list);
     JSONObject resultObject = new JSONObject(){{
          put("total",pageInfo.getTotal());
          put("list",pageInfo.getList());
          put("pageNum",pageInfo.getPageNum());
        }}
        return RespEntity.success(resultObject);
}

@RequestPart

  • @RequestPart 注解可以用来替换 @RequestParam 接收文件以及其他更为复杂的数据类型(json xml等等)
  • 注意:这里如果设置了value的话,前端传参的参数名也必须一样,否则拿不到值。
  • 可以有多个

@RequestPart-同时上传文件和json的解决方案

1、使用@RequestPart+@RequestParam

  • 通过@RequestParam("josnData") String jsonData 和@RequestPart("uploadFile") MultiPartFile uploadFile 两个注解分别接受参数
  • 使用@RequestParam(“josnData”)注解接受的类型只能为String,需要自行进行JSON解析String,不过能实现POST上传文件类型参数和json格式字符串参数。
@PostMapping("/uploadFile")
public JsonResult uploadFile(@RequestPart("uploadFile") MultipartFile[] uploadFiles, @RequestParam("jsonData") String jsonData){
}

2、只使用@RequestPart

  • 下面这种则使用@RequestPart注解接受JSON数据,此时可以将接收的json字符串直接序列化为实例对象
  • 注意:此时json字符串一定要声明类型 Content-Type: application/json,否则使用@RequestPart注解无法反序列化
@PostMapping("/jsonDataAndUploadFile")
public String jsonDataAndUploadFile(@RequestPart("uploadFile") List<MultipartFile> uploadFiles,
                           @RequestPart("jsonData") Person person) {

}

@RequestBody

  • POST请求
  • 注解@RequestBody接收的参数是来自requestBody中,即请求体。一般用于处理非 Content-Type: application/x-www-form-urlencoded编码格式的数据,比如:application/json、application/xml等类型的数据。
  • 只能有一个

实例

@PostMapping("/saveStudent")
public JsonResult saveStudent(@RequestBody Student student) {
   studentService.save(student);
   return JsonResult.success();
}

**

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值