概念
Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
功能
资源:互联网所有的事物都可以被抽象为资源
资源操作:使用POST、DELETE、PUT、GET,使用不同方法 对资源进行操作。
分别对应 添加、 删除、修改、查询。
我们先看一下浏览器中一些百度百科中的地址这就是RestFul风格.
这种风格不是与生俱来的,也需要演变.
下面说说原始风格的.
代码:
package com.qiu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RestfulContraller {
//http://localhost:8080/add?a=1&b=2 之前的写法
@RequestMapping("/add")
public String test(int a, int b , Model model){
int res = a+b;
model.addAttribute("msg","结果为"+res);
return "test";
}
}
在浏览器中,如果直接输入/add则会出现500错误
这个时候我们要传入参数
这样结果才出来了,但是这种写法太过于繁琐,所以就有了RestFul风格
代码:
package com.qiu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RestfulContraller {
//http://localhost:8080/add?a=1&b=2 之前的写法
//RestFul :http://localhost:8080/add/a/b
@RequestMapping("/add/{a}/{b}")
public String test(@PathVariable int a, @PathVariable int b , Model model){
int res = a+b;
model.addAttribute("msg","结果为"+res);
return "test";
}
}
这样就减少了繁琐的输入.