-
REST(Respresentational State Transfer),表现形式状态转换
- 传统风格资源描述:
- http://localhost/user/getById?id=1
- http://localhost/user/saveUser
- REST风格描述形式
- http://ocalhost/user/1
- http://locahost/user
- 传统风格资源描述:
-
优点:
- 隐藏资源的访问行为,无法通过地址得知资源是何种操作
- 书写简化
-
按照REST风格访问资源时使用行为动作区分对资源进行了何种操作
- GET(查询)
- POST(新增/保存)
- DELETE(删除)
- PUT(修改/更新)
-
上述行为是约定方式,约定不是规范,可以打破,所以是REST风格,而不是规范
-
描述模块的名称通常使用复数,也就是加s的格式描述,表示此类资源,而非单个资源,例如:users,books,accounts
-
根据REST风格对资源进行访问成为RESTful
@Controller
public class UserController {
//添加请求方法,value可以省略
@RequestMapping(value = "/users", method = RequestMethod.POST)
@ResponseBody
public String save(User user) {
System.out.println("user save ... " + user);
return "{'module':'user save'}";
}
@RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
@ResponseBody
// @PathVariable表示变量在路径上,同时把路径名写成/find/{id}
public String delete(@PathVariable Integer id) {
System.out.println("user delete ... " + id);
return "{'module':'user delete'}";
}
@RequestMapping(value = "/users", method = RequestMethod.PUT)
@ResponseBody
public String update(User user) {
System.out.println("user update ... " + user);
return "{'module':'user update'}";
}
// @RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
// @ResponseBody
// public String find(@PathVariable Integer id) {
// System.out.println("user save ... " + id);
// return "{'module':'user find'}";
// }
@RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
@ResponseBody
public String getById(@PathVariable Integer id) {
System.out.println("user get by id: ... " + id);
return "{'module':'getById'}";
}
@RequestMapping(value = "/users", method = RequestMethod.GET)
@ResponseBody
public String getAll() {
System.out.println("user get all ... ");
return "{'module':'get all'}";
}
}
@RequestBody @RequestParam @PathVariable
- 区别:
- @RequestParam用于接收url地址传参或表单传参
- @RequestBody用于接收json数据
- @PathVariable用于接收路劲参数,使用{参数名称}描述路劲参数
- 应用:
- 后期开发中,发送请求参数超过1个时,以json格式为主,@RequestBody应用较广
- 如果发送非json数据格式,选用@RequestParam接收请求参数
- 采用RESTful进行开发,当参数数量较少时,例如一个,可以采用@PathVariable接收请求路径变量,通常用于传递id值
//@Controller
//@ResponseBody
//上面两个注解可以简化成@RestController注解
@RestController
@RequestMapping("/books")
public class BookController {
//添加请求方法,value可以省略
// @RequestMapping(method = RequestMethod.POST)
//上面的注解可以简化成下面的注解
@PostMapping
public String save(User user) {
System.out.println("user save ... " + user);
return "{'module':'user save'}";
}
// @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@DeleteMapping("/{id}")
public String delete(@PathVariable Integer id) {
System.out.println("user delete ... " + id);
return "{'module':'user delete'}";
}
// @RequestMapping(method = RequestMethod.PUT)
@PutMapping
public String update(User user) {
System.out.println("user update ... " + user);
return "{'module':'user update'}";
}
// @RequestMapping(value = "/{id}", method = RequestMethod.GET)
@GetMapping("/{id}")
public String getById(@PathVariable Integer id) {
System.out.println("user get by id: ... " + id);
return "{'module':'getById'}";
}
// @RequestMapping(method = RequestMethod.GET)
@GetMapping
public String getAll() {
System.out.println("user get all ... ");
return "{'module':'get all'}";
}
}
- IDEA中多个光标:Shift+Alt+鼠标左键
@RestController
@RequestMapping("/books")
public class BookController {
@PostMapping
public String save(@RequestBody Book book) {
System.out.println("book save ==>" + book);
return "{'module':'book save ... success'}";
}
@GetMapping
public List getAll() {
Book book1 = new Book();
book1.setType("计算机");
book1.setName("SpringMVC入门教程");
book1.setDescription("小试牛刀");
Book book2 = new Book();
book2.setType("计算机");
book2.setName("SpringMVC实战教程");
book2.setDescription("一代宗师");
List bookList = new ArrayList<>();
bookList.add(book1);
bookList.add(book2);
return bookList;
}
}
2.设置对静态资源的访问放行
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
@Override
//添加资源过滤器
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
//当访问/pages/???的时候,走/pages目录下的内容
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
}
}
@Configuration
@ComponentScan({"com.itheima.controller", "com.itheima.config"})
@EnableWebMvc
public class SpringMvcConfig {
}
3.前端页面通过异步提交访问后台控制器
//添加
saveBook() {
axios.post("/books", this.formData).then((res) => {
});
},
//主页列表查询
getAll() {
axios.get("/books").then((res) => {
this.dataList = res.data;
});
},



