序言:
@RestController (@Controller @ResponsetBody的组合)
@RequestBody (前端传的参数(post请求) )
@RequestParam Map<String, Object> parames(get请求参数接收方式)
@RequestMapping (“/user” 路由的统一前缀)
@PathVariable ( /user/1 路径参数注解)
UserController.class
package com.example.wade.controller;
import com.example.wade.domain.User;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/user")
public class UserController {
// 搜索用户
@GetMapping("/search")
public String searchUsers(@RequestParam Map<String, Object> parames) {
System.out.println("搜索成功"+ parames.get("name")+ parames.get("age"));
return "{'code': 0, 'msg': '搜索用户成功'}";
}
// 查询所有用户
@GetMapping
public String getUsers() {
System.out.println("请求列表成功");
return "{'code': 0, 'msg': '请求列表成功'}";
}
// 查询指定用户
@GetMapping("/{id}")
public String getUserById(@PathVariable Integer id) {
System.out.println("查询指定用户成功"+ id);
return "{'code': 0, 'msg': '查询指定用户成功+ '}"+id;
}
// 更新指定用户
@PutMapping("/{id}")
public String setUserById(@PathVariable Integer id) {
System.out.println("更新指定用户"+ id);
return "{'code': 0, 'msg': '更新指定用户成功+ '}"+id;
}
// 删除指定用户
@DeleteMapping("/{id}")
public String delteUserById(@PathVariable Integer id) {
System.out.println("删除用户成功"+ id);
return "{'code': 0, 'msg': '删除用户成功+ '}"+id;
}
// 保存用户
@PostMapping
public String save(@RequestBody User user) {
System.out.println("添加用户+。。。。。"+ user);
return "{'code': 0, 'msg': '保存数据成功'}";
}
}
User.class
package com.example.wade.domain;
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}