描述访问网络资源的格式
传统风格:http://localhost/user/saveUser
rest风格:http://localhost/user
优点
1.隐藏资源访问行为(用行为动作区分操作)
2.书写简化
入门案例(最基础,有不合理)
java
@RequestMapping(value = "/users", method = RequestMethod.POST)
@ResponseBody
public String save(@RequestBody User user) {
System.out.println("user save " + user);
return "{'module':'user save'}";
}
@RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
@ResponseBody
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(@RequestBody User user) {
System.out.println("user update " + user);
return "{'module':'user update'}";
}
@RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
@ResponseBody
public String getById(@PathVariable Integer id) {
System.out.println("user getById " + id);
return "{'module':'user getById'}";
}
@RequestMapping(value = "/users",method = RequestMethod.GET)
@ResponseBody
public String getAll() {
System.out.println("user getAll ");
return "{'module':'user getAll'}";
}
快速开发
java
package org.example.controller;/*
* @Auther:huangzhiyang
* @Date:2023/10/7
* @Description:
*/
import org.example.domain.Book;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/book")
public class BookController {
@PostMapping
public String save(@RequestBody Book book) {
System.out.println("book save "+book);
return "{'info':'springmvc'}";
}
@DeleteMapping("/{id}")
public String delete( @PathVariable String id) {
System.out.println("book delete"+id);
return "{'info':'springmvc'}";
}
@PutMapping
public String update(@RequestBody Book book) {
System.out.println("book update");
return "{'info':'springmvc'}";
}
}