SpringMVC-RestFul Style
三、RestFul Style
概念
RestFul是一个资源定位及资源操作的风格,不是标准也不是协议,只是一种风格,基于这种风格设计的软件可以更简洁,更有层次感,更易于实现缓存等机制;
功能
- 资源:互联网上所有事物都可以称为资源;
- 资源操作:使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作;
- 分别对应:添加、删除、修改、查询。
默认风格:
@Controller
public class RestFulStyleDemo{
@RequestMapping("/add")
public String getStyle(int a, int b, Model model){
int c = a+b;
model.addAttribute("msg","结果为:"+c);
return "test";
}
}
访问:
http://localhost:8080/add?a=1&b=2;
RestFul风格:
@Controller
public class RestFulStyleDemo02 {
@RequestMapping(name="value="/add/{a}/{b}",method= RequestMethod.GET)
public String getStyle(@PathVariable int a,@PathVariable int b, Model model){
int c = a+b;
model.addAttribute("msg","结果为:"+c);
return "test";
}
}
访问:
http://localhost:8080/add/1/2