SpringMVC接收请求参数

后端 0 637
小小草
小小草 2023年3月27日 22:37 发表

1.@PathVariable 全称是url占位符

  @GetMapping("/pathVariable2/{id}")
  public String pathVariable2(@PathVariable(value = "id")
      String id){
    return id;
  }

调用:http://localhost:8082/pathVariable2/2

 

2.传递字符串参数

-1 不带注解

  @GetMapping("/requestParam")
  public String requestParam(String userName,String age){
    return userName+":xxx:"+age;
  }

1)http://localhost:8082/requestParam 返回 null:xxx:null

2)http://localhost:8082/requestParam?userName=zhangsan 返回 zhangsan:xxx:null

3)http://localhost:8082/requestParam?userName=zhangsan&age=11  返回 zhangsan:xxx:11

以上说明,参数可传可不传

@PostMapping("/requestParamPost")
  public String requestParamPost(String userName,String age){
    return userName+":xxx:"+age;
  }

(注意是postman的post请求调用)post请求:http://localhost:8082/requestParam?userName=zhangsan&age=11 对比发现,如果是字符串参数,get post请求效果一样

-2 使用@RequestParam注解

  @GetMapping("/requestParam2")
  public String requestParam2(@RequestParam("uname") String userName,
      @RequestParam String age){
    return userName+":xxx:"+age;
  }

http://localhost:8082/requestParam2?uname=zhangsan&age=11

  @PostMapping("/requestParam2")
  public String requestParam2(@RequestParam("uname") String userName,
      @RequestParam String age){
    return userName+":xxx:"+age;
  }

(注意是postman的post请求调用)post请求:http://localhost:8082/requestParam2?uname=zhangsan&age=11

-3 使用@RequestBody注解

它就不能支持get请求了,所以这里只有post请求,注意:只支持单个参数

  @PostMapping("/RequestBody2")
  public String requestBody2(@RequestBody String name){
    return "name is:"+name;
  }

 

 

 

3.传递对象参数

-1 不带注解 

  @GetMapping("/requestParam3")
  public String requestParam3(User user){
    return user.getId()+":xxx:"+user.getName();
  }

http://localhost:8082/requestParam3?name=zhangsan&id=11

  @PostMapping("/requestParam4")
  public String requestParam4(User user){
    return user.getId()+":xxx:"+user.getName();
  }

(注意是postman的post请求调用)http://localhost:8082/requestParam4?name=zhangsan&id=11

除了请求方式外和get是一样的,同时传递字符串参数和对象也是一样的

-2 @RequestParam注解

注意:请不要这么做,要么不加注解,要么用下面的方式

-3 @RequestBody注解

老规矩,还是只支持post请求

@PostMapping("/requestBody3")
public String requestBody3(@RequestBody User user){
return "user is:"+ JSON.toJSONString(user);
}

 

4.传递数组

-1 不带注解

  @GetMapping("/requestParam5")
  public String requestParam5(String[] aa){
    return JSON.toJSONString(aa);
  }

http://localhost:8082/requestParam5?aa=1&aa=2

  @PostMapping("/requestParam6")
  public String requestParam6(String[] aa){
    return JSON.toJSONString(aa);
  }

(postman调用post方式) http://localhost:8082/requestParam6?aa=1&aa=2

-2  @RequestParam

方式和效果跟上面完全一样,同时字符串的传递,加不加@RequestParam的效果和做法都一样,加了就多了必传的效果

-3 @RequestBody

  @PostMapping("/requestParam6")
  public String requestParam6(@RequestBody String[] aa){
    return JSON.toJSONString(aa);
  }

 

 

 5.传递List

推荐使用@RequestBody

  @PostMapping("/listParam1")
  public String listParam1(@RequestBody List<String> aa){
    return JSON.toJSONString(aa);
  }

 

 

  @PostMapping("/listParam2")
  public String listParam2(@RequestBody List<User> aa){
    return JSON.toJSONString(aa);
  }

 

 6.传递map

这里说下,map和对象其实本质是一样的

  @PostMapping("/mapParam1")
  public String mapParam1(@RequestBody Map<String,String> map){
    return JSON.toJSONString(map);
  }

 

 

  @PostMapping("/mapParam2")
  public String mapParam2(@RequestBody Map<String,User> map){
    return JSON.toJSONString(map);
  }

 

扩展:https://blog.csdn.net/a654540233/article/details/84936908

点赞 0 收藏(0)    分享
相关标签: spring mvc
问题没解决?让chatGPT帮你作答 智能助手
0 个评论
  • 消灭零评论