REST风格
REST简介
REST(Representational State Transfer),表现形式状态转换
传统风格资源描述形式
http://localhost/user/getById?id=1
http://localhost/user/saveUser
REST风格描述形式
http://localhost/user/1
http://localhost/user
优点:
隐藏资源的访问行为,无法通过地址得知对资源是何种操作
书写简化
按照REST风格访问资源时使用行为动作区分对资源进行了何种操作
http://localhost/users 查询全部用户信息 GET(查询)
http://localhost/users/1 查询指定用户信息 GET(查询)
http://localhost/users 添加用户信息 POST(新增/保存)
http://localhost/users 修改用户信息 PUT(修改/更新)
http://localhost/users/1 删除用户信息 DELETE(删除)
注:
上述行为是约定方式,约定不是规范,可以打破,所以称REST风格,而不是REST规范
描述模块的名称通常使用复数,也就是加s的格式描述,表示此类资源,而非单个资源,例如:users、books...
RESTful
根据REST风格对资源进行访问称为RESTful
案例
第一步、设定http请求动作(动词)
GET(查询)
@RequestMapping(value = "/users",method = RequestMethod.GET)
POST(新增/保存)
@RequestMapping(value = "/users",method = RequestMethod.POST)
PUT(修改/更新)
@RequestMapping(value = "/users",method = RequestMethod.PUT)
DELETE(删除)
@RequestMapping(value = "/users",method = RequestMethod.DELETE)
第二步、设定请求参数(路径变量)
@RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)@ResponseBodypublic String delete(@PathVariable Integer id){System.out.println("User " + id +"delete!");return "{'module' : 'user delete'}";}
Mybatis五种基本操作对应RESTful:
@RequestMapping(value = "/users",method = RequestMethod.POST)@ResponseBodypublic String save(){System.out.println("User saving!");return "{'module':'user save'}";}@RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)@ResponseBodypublic String delete(@PathVariable Integer id){System.out.println("User " + id +"delete!");return "{'module' : 'user delete'}";}@RequestMapping(value = "/users",method = RequestMethod.PUT)@ResponseBodypublic String update( User user){System.out.println("User updating!!");return "{'module':'user update'}";}@RequestMapping(value = "/users/{id}",method = RequestMethod.GET)@ResponseBodypublic String getById(@PathVariable Integer id){System.out.println("User " + id + "get by id" );return "{'module':'user getById'}";}@RequestMapping(value = "/users",method = RequestMethod.GET)@ResponseBodypublic String getAll(){System.out.println("User get all" );return "{'module':'user getAll'}";}
@RequestBody @RequestParam @PathVariable
区别
@RequestParam 用于接收url地址传参或表单传参
@RequestBody 用于接收json数据
@PathVariable 用于接受路径参数,使用{参数名称}描述路径参数
应用
后期开发中,发送请求参数超过一个时,以json格式为主,@RequestBody应用较广
如果发送非json格式数据,选用@RequestParam接收参数
采用RESTful进行开发,当参数数量较少时,例如一个,可以采用@PathVariable接收请求路径变量,通常用于传递id值
RESTful简化开发
@RestController
@RequestMapping("/user")
public class UserController {@PostMappingpublic String save(){System.out.println("User saving!");return "{'module':'user save'}";}@DeleteMapping("/{id}")public String delete(@PathVariable Integer id){System.out.println("User " + id +"delete!");return "{'module' : 'user delete'}";}@PutMappingpublic String update( User user){System.out.println("User updating!!");return "{'module':'user update'}";}@GetMapping("{id}")public String getById(@PathVariable Integer id){System.out.println("User " + id + "get by id" );return "{'module':'user getById'}";}@GetMappingpublic String getAll(){System.out.println("User get all" );return "{'module':'user getAll'}";}
}