springcloud 在controller中什么时候用户PostMapping,什么时候用GetMapping

PostMapping("/xx/xxx")

  1. 传的参是@RequestBody,多参或对对象

GetMapping("/xx/xxx")

  1. 无参时
  2. 传的参是@RequestParam
  3. 传的参是@PathVariable

@RequestParam

@RequestMapping("show16")
    public ModelAndView test16(@RequestParam("name")String name){}
@RequestMapping("show17")
    public ModelAndView test17(@RequestParam(value="name",required=false)String name){}
 @RequestMapping("show18")
    public ModelAndView test18(@RequestParam(value="name",required=true,defaultValue="hello")String name){}

@PathVariable 映射 URL 绑定的占位符


@RequestMapping(value = "user/{username}")
    public String userProfile(@PathVariable(value="username") String username) {}

@PathVariable 和 @RequestParam 综合使用

@RequestMapping(value = "/user/{userid}/view")
public String query(@PathVariable("userid") String userid,@RequestParam(value="username") String username ) {	
	System.out.println("userid= "+userid);   //此处可以获取:userid= 031267
	System.out.println("username = " + username);   //此处打印:username = zhangsan
	return SUCCESS;
}

@RequestBody

 @PostMapping("/listForOrder")
    public List<ProductInfoOutput> listForOrder(@RequestBody List<String> productIdList) {)
发布了283 篇原创文章 · 获赞 11 · 访问量 14万+

猜你喜欢

转载自blog.csdn.net/liuming690452074/article/details/104520505