@RequestParam(加@RequestBody)、@PathParam、@PathVariable

请求:localhost:8088/jiu/update/4?a=a&bb=b

@GetMapping("/update/{ids}")
public void updateUser(@PathVariable("ids") int id, @PathParam("aa") int a, @RequestParam(value = "bb", required = false) String b){
      System.out.println(id);//4
      System.out.println(a);//a
      System.out.println(b);//b
}

@RequestParam与@RequestBody

  • @RequestBody是接受前端post提交传来的Json格式字符串,后端可以用实体类或者是String进行接收。
@RequestMapping("/test4")
@ResponseBody
public String fn4(@RequestBody User user, @RequestParam(name="aaa", required = true, defaultValue = "12312") String code) {
      System.out.println(user);
      System.out.println(code);
      return user.toString() + ">>>" + code;
  }

请求路径:localhost:8088/jiu/test4?aaa=a
图:

@RequestParam(name="aaa", required = false, defaultValue = "10086") String code

  • name应该和前端get中提交的键相同,不写则默认后边的名称(code)
  • required默认TRUE 地址栏必须有对应参数 否则报错 但是设置有defaultValue这个属性情况就不同了 地址栏没有传对应参数也可以
  • defaultValue 地址栏中没传值 或是匹配不上 就给他安排一个默认值

@PathParam

  • PathParam 与 RequestParam都是在地址栏中取get提交的键值对中的值。(注意:@PathParam("aa") int a中对应取的是地址栏中键为a的值,当a为int时,如果没有a对应的键值对,就会报错;当a为String类型时,不会报错,会自动有一个默认的值。其他类型未测试)

@PathVariable

  • 用于RestFul风格的get请求,在这种@GetMapping("/update/{ids}")URL模板里边取,且PathVariable后边名字必须和模板中相同。
    eg:public void updateUser(@PathVariable("ids") int id,
posted @ 2022-08-10 16:48  九月!!  阅读(201)  评论(0)    收藏  举报