Back End

RESTFul风格请求

PineappleCat · 9月6日 · 2021年 97次已读
  • 只能使用名词不能使用动词
  • DELETE PUT POST GET
  • GET请求不能改变资源状态
  • 所有资源一直保持使用复数形式

GET

/**
   * 直接GET请求,获取car的集合
   *
   * @return
   */
  @GetMapping("/users")
  public User get() {
    return User.builder().username("liuzhi").age(30).build();
  }

  /**
   * 表单GET请求,返回单个user
   *
   * @param user
   * @return
   */
  @GetMapping("/users/user")
//  public User getByName(String username,String age)  等价
//  public User getByName(@RequestParam("username") String username,@RequestParam("age") String age)  等价
//   @RequestParam(value="username", required=false) 是否必须
  public User getByName(User user) {
    System.err.println(user.toString());
    user.setUsername("liuzhi2");
    user.setAge(30);
    return user;
  }

  /**
   * GET请求,路径上获取参数
   *
   * @param name
   * @return
   */
  @GetMapping("/users/{name}")
  public void getByName(@PathVariable(value = "name") String name) {
    System.err.println(name);
  }

DELETE

/**
 * DELETE请求 删除用户
 *
 * @param username
 */
@DeleteMapping("/users/{username}")
public void delete(@PathVariable(value = "username") String username) {
  System.err.println(username);
}

POST

/**
 * POST请求,添加用户
 *
 * @param user
 * @return
 */
@PostMapping("/users")
public User addUser(@RequestBody User user) {
  System.err.println(user.toString());
  return user;
}

PUT

/**
 * PUT请求 修改用户信息
 *
 * @param user
 */
@PutMapping("/users")
public User modify(@RequestBody User user) {
  System.err.println(user.toString());
  return user;
}

Click here to view the copyright notice of this site(点击此处查看本站版权声明)
0 条回应

必须 注册 为本站用户, 登录 后才可以发表评论!