一、用REST风格,来演示"增、删、改、查"操作。
1.1:增POST
1.1.1:用PostMan测试:
增POST:代码块
@RequestMapping(value = "/users", method = RequestMethod.POST)
@ResponseBody
public String save() {
System.out.println("user save...");
return "module: User save...";
}
1.2:删DELETE:
(删是有"参数"的,比较复杂。)
(解析:1-需要在"行参"里面加上:@PathVariable)
(解析:2-这样"行参"的id就可以和:"value = "/users/{id}"的id联系起来)
(解析:3-联系图片进行"理解")
1.2.1:用PostMan测试:
删DELETE:代码块
@RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable Integer id) { //形参里面加上@PathVariable注解,和"/users{id}"呼应
System.out.println("user delete..." + id);
return "module: User delete...";
}
1.3:改PUT:(PUT复杂的是:传参)
(改是有"参数"的,而且参数是"实体类",比较复杂)
(解析:1-需要在"行参"里面加上:@RequestBody,加上这个才可以传入一个"实力类")
(解析:2-"value = "/users", method = RequestMethod.PUT"这个不需要参数,因为"实体类",传入的数据需要是"JSON"数据,在PostMan进行)
(解析:3-联系图片进行"理解")
1.3.1:用PostMan测试:
改PUT:代码块
@RequestMapping(value = "/users", method = RequestMethod.PUT)
@ResponseBody
public String update(@RequestBody User user) {
System.out.println("user update..." + user);
return "module: User update...";
}
1.4:查GET
(思路和操作和上面一样,这边不多做解释)
1.4.1:PostMan测试(按Id查):
1.4.2:PostMan测试(全查):
查GET:代码块(按id查、全查)
@RequestMapping(value = "/users/{id}",method = RequestMethod.GET) //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...";
}
二、总结:
//-------------------------------------------------------------------
//REST风格
//增
@RequestMapping(value = "/users", method = RequestMethod.POST)
@ResponseBody
public String save() {
System.out.println("user save...");
return "module: User save...";
}
//删
@RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable Integer id) { //形参里面加上@PathVariable注解,和"/users{id}"呼应
System.out.println("user delete..." + id);
return "module: User delete...";
}
/*
--> 上面传入单个参数
传入"单个参数"和"传入一个类"写法是不一样的。
--> 下面传入一个"类",PostMan传参不一样
*/
//改
@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) //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...";
}
标签:...,users,springMVC08,REST,User,user,value,id,入门
From: https://www.cnblogs.com/chen-zhou1027/p/16986263.html