RestFul风格
RestFul就是一个资源定位及资源操作的风格,它不是标准也不是协议,基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
传统资源操作方式:
- http://127.0.0.1/project/findUser.do?id=1 查询 GET
- http://127.0.0.1/project/addUser.do 新增 POST
- http://127.0.0.1/project/modifyUser.do 更新 POST
- http://127.0.0.1/project/deleteUser.do?id=1 删除 GET或POST
使用RestFul风格:
- http://127.0.0.1/project/1 查询 GET
- http://127.0.0.1/project 新增 POST
- http://127.0.0.1/project 更新 POST
- http://127.0.0.1/project/1 删除 GET或POST
常用的资源操作:
POST、DELETE、PUT、GET
package com.sw.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @Author suaxi
* @Date 2020/12/17 11:25
*/
@Controller
public class RestFulController {
//传统风格 http://localhost:8088/add01?a=1&b=1
@RequestMapping(value = "/add01/{a}/{b}",method = RequestMethod.GET)
public String test01(@PathVariable int a,@PathVariable int b, Model m){
int result = a + b;
m.addAttribute("msg","结果1为:"+result);
return "hello";
}
//简化 http://localhost:8088/add02/1/2
@PostMapping("/add02/{a}/{b}")
public String test02(@PathVariable int a,@PathVariable int b, Model m){
int result = a + b;
m.addAttribute("msg","结果2为:"+result);
return "hello";
}
}
注:@PathVariable 让方法参数的值对应绑定到一个URI模板变量上
评论 (0)