个人主页: 叶落闲庭
我的专栏:
c语言
数据结构
javaweb
石可破也,而不可夺坚;丹可磨也,而不可夺赤。
http://localhost/user/getById?id=1
http://localhost/user/saveUser
http://localhost/user/1
http://localhost/user
http://localhost/users
查询全部用户信息 - - GET(查询)http://localhost/users/1
查询指定用户信息 - - GET(查询)http://localhost/users
添加用户信息 - - POST(新增/保存)http://localhost/users
修改用户信息 - - PUT(修改/更新)http://localhost/users/1
删除用户信息 - - DELETE(删除)@RequestMapping(value = "/users", method = RequestMethod.GET)
@ResponseBody
public String save() {
System.out.println("use save...");
return "'module':'user save'";
}
//修改
@RequestMapping(value = "/users", method = RequestMethod.PUT)
@ResponseBody
public String update(@RequestBody User user) {
System.out.println("use update..." + user);
return "'module':'user update'";
}
//带参数
@RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable int id) {
System.out.println("use delete..." + id);
return "'module':'user delete'";
}
@RequestMapping
@RequestMapping(value = "/users", method = RequestMethod.GET)
@ResponseBody
public String save() {
System.out.println("use save...");
return "'module':'user save'";
}
@PathVariable
@RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable int id) {
System.out.println("use delete..." + id);
return "'module':'user delete'";
}
@RequestParam
用于接收url地址传参或表单传参@ResponseBody
用于接收json数据@PathVariable
用于接收路径参数,使用(参数名称)描述路径参数@ResponseBody
应用较广@RequestParam
接收请求参数@PathVariable
接收请求路径变量,通常用于传递id值@RestController
@Controller
和@ResponseBody
两个注解组合功能@RestController
@RequestMapping("/address")
public class BookController {
}
@GetMapping
、@PostMapping
、@DeleteMapping
、@PutMapping
@GetMapping
对应GET请求@RestController
@RequestMapping("/address")
public class BookController {
@GetMapping
public String save() {
System.out.println("use save...");
return "'module':'user save'";
}
@DeleteMapping
public String delete() {
System.out.println("use delete...");
return "'module':'user delete'";
}
//修改
@PutMapping
public String update(@RequestBody Address address) {
System.out.println("use update..." + address);
return "'module':'user update'";
}
@GetMapping("{id}")
public String getById(@PathVariable int id){
System.out.println("address getById..." + id);
return "'module':'address getById'";
}
}
通过这篇文章,基于SpringMVC的RESTful快速开发就介绍到这里了,欢迎各位小伙伴点赞+关注!!!