概念: Restful 就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
功能:
POST、DELETE、PUT、GET,使用不同方法对资源进行操作。添加、删除、修改、查询传统方式操作资源: 通过不同的参数来实现不同的效果,这样的话 方法单一,就只要 原生态的 POST 和 GET。
查询操作 GET新增 POST更新 POST删除 GET 或 POST 使用 RestFul 操作资源: 可以通过不同的请求方式来实现不同的效果!如下:请求地址一样,但是功能可以不同!
查询 GET新增 POST更新 PUT删除 DELETE@RequestMapping(path="请求路径/{参数1}/{参数2}",method=RequestMethod.GET):以 GET 方式 去 发送 这个请求。
path的话可以 写为 value,但是 不方便理解和记忆。所以最好是写path。
当然还有如下四个衍生 注解:使用起来 会 更加的方便。
@GetMapping("请求路径/{参数1}/{参数2}")
@PostMapping("请求路径/{参数1}/{参数2}")
@PutMapping("请求路径/{参数1}/{参数2}")
@DeleteMapping("请求路径/{参数1}/{参数2}")
这个地方 说的 参数1 和 参数2 指的就是 当前 这个请求方法 的 形参。只不过 你必须 用 @PathVariable 去修饰它们。
package top.muquanyu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class RestFulController {
@RequestMapping(path="/add/{a}/{b}",method = RequestMethod.GET)
public String test1(@PathVariable int a, @PathVariable int b, Model model){
int res = a + b;
model.addAttribute("msg","结果为:" + res) ;
return "hello";
}
}

@GetMapping("/add/{a}/{b}")
public String test2(@PathVariable int a, @PathVariable int b, Model model){
int res = a + b;
model.addAttribute("msg","结果为:" + res) ;
return "hello";
}
优点:
请求方法的形参 也是 我们 发送请求的时候,路径里面 的 参数。它们之间是一一对应的。所以我们 设置完参数后,直接就可以 使用传统风格。
package top.muquanyu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RestFulController {
@RequestMapping("/add")
public String test1(int a, int b, Model model){
int res = a + b;
model.addAttribute("msg","结果为:" + res) ;
return "hello";
}
}
如果你要是 直接 请求 add 这个路径,肯定会报错,因为我们 已经在这个请求方法里 设定 a 和 b 这两个参数了。所以你在 请求的时候必须携带这两个参数。

