分析restful风格:
特点·:开发简单 简洁高效 安全性高
分析:很多格式就是localhost:8080/add/a/b/c…一直使用/来进行操作传入我们所需要的数据 比如百度百科的地址框:https://baike.baidu.com/item/CSDN/172150?fr=aladdin
格式都是item后面加上搜索的关键字 类似这种的风格
//@RequestMapping(value="/add/{a}/{b}",method=RequestMethod.Get)
@GetMapping("/add/{a}/{b}")
public String testrest(@PathVariable int a, @PathVariable int b, Model model){
int res=a+b;
model.addAttribute("msg","RestController: "+res);
return "test";
}
这里我们传入的url地址加入了/a/b 通过@PathVariable注解 将对应参数的数据从地址栏传给对应的参数 作为我们的数据交给我们的方法处理,这样就可以将数据进行隐藏。
还有就是**GetMapping()**注解:
@GetMapping("/add/{a}/{b}"):直接传入的是url
@RequestMapping(value="/add/{a}/{b}",method=RequestMethod.Get):
以上两个注解是等价的 以上就是地址的url和请求的方式类似的PostMapping注解DeleteMapping...
这里分析针对同一个url我们方法上只能允许一个Mapping方式提交,比如说:
//@RequestMapping(value="/add/{a}/{b}",method=RequestMethod.Get)
@GetMapping("/add/{a}/{b}")
public String testrest1(@PathVariable int a, @PathVariable int b, Model model){
int res=a+b;
model.addAttribute("msg","RestController: "+res);
return "test";
}
@PostMapping("/add/{a}/{b}")
public String testrest2(@PathVariable int a, @PathVariable int b, Model model){
int res=a+b;
model.addAttribute("msg","RestController: "+res);
return "test";
}
就那这个例子说:这个时候如果我们在地址栏里打入/add/x/x的uri就会执行第一个方法,表单提交默认使用的是get,所以就会执行第一个方法