REST风格的表现形式转化
-
传统风格资源描述形式
-
REST风格描述形式
-
优点
-
隐藏资源的访问行为,无法通过地址得知对资源是何种操作
-
书写简化
-
-
按照REST风格访问资源时使用行为动作区分
-
http://localhost/users 查询全部用户信息 GET (查询)
-
http://localhost/users 添加用户信息 POST (添加)
-
http://localhost/users 修改用户信息 PUT (修改)
-
http://localhost/users/1 查询指定用户信息 GET (查询)
-
http://localhost/users/1 删除指定用户 DELETE (删除)
-
-
步骤
-
@RequestMapping的属性
- value(默认): 设置请求访问路径
- method:http请求动作,标准动作(GET/POST/PUT/DELETE)
-
设定http请求动作
- @RequestMapping(value = "/users",method = RequestMethod.POST)
- @GetMapping @PostMapping @PutMapping @DeleteMapping
-
设定请求参数
- @RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
- @GetMapping("/{id}")
- @PathVariable 绑定路径参数与形参名
- @RequestBody 用于接收JSON数据
- @RequestParam 用户接收url地址传参或表单传参
-
-
案例
@RestController
@RequestMapping("/users")
public class UserController {
//@RequestMapping(value = "/users",method = RequestMethod.POST)
@PostMapping
public String save(@RequestBody User user){
System.out.println("user save ..."+ user);
return "{'module':user save}";
}
//@RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
@DeleteMapping("/{id}")
public String delete(@PathVariable Integer id){
System.out.println("user delete ..."+ id);
return "{'module':user delete}";
}
//@RequestMapping(value = "/users",method = RequestMethod.PUT)
@PutMapping
public String update(@RequestBody User user){
System.out.println("user update ..."+ user);
return "{'module':user update}";
}
//@RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println("user getById ..."+ id);
return "{'module':user getById}";
}
//@RequestMapping(value = "/users",method = RequestMethod.GET)
@GetMapping
public String getAll(){
System.out.println("user getAll ...");
return "{'module':user getAll}";
}
}
标签:http,users,简介,REST,风格,user,id,localhost,RequestMapping
From: https://www.cnblogs.com/-xyk/p/17589053.html