RESTFulTestController.java 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.malk.test;
  2. import com.malk.server.common.McR;
  3. import com.malk.test.dto.User;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.apache.poi.ss.formula.functions.T;
  6. import org.springframework.context.annotation.Profile;
  7. import org.springframework.web.bind.annotation.*;
  8. import java.util.*;
  9. /**
  10. * RESTFul 风格 [取值详见CatchException]
  11. * -
  12. * 1. GET >>> /users >>> 查询用户列表
  13. * 2. POST >>> /users >>> 创建一个用户
  14. * 3. GET >>> /users/id 查询一个用户
  15. * 4. PUT/PATCH >>> /users/id 更新一个用户 [PUG全量更新, PATC增量更新]
  16. * 5. DELETE >>> /users/id 删除一个用户
  17. * -
  18. * 参数获取方式均相同, GET 没有 body, @PathVariable 获取 url 占位符, RequestParam 获取 url 参数, @RequestBody 获取 body 数据
  19. */
  20. @Slf4j
  21. @Profile({"dev", "test"})
  22. @RestController
  23. @RequestMapping("/test/users")
  24. public class RESTFulTestController {
  25. // 创建线程安全的Map
  26. static Map<Long, User> users = Collections.synchronizedMap(new HashMap<>());
  27. // 注解参考 MCTestController
  28. @ModelAttribute
  29. void userId(User user) {
  30. long id1 = (long) (Math.random() * 100);
  31. long id2 = new Random().nextInt(1000);
  32. log.info("Random, {}, {}", id1, id2);
  33. user.setId(id1 + id2);
  34. user.setName(String.valueOf(id2));
  35. user.setAge((int) id1);
  36. }
  37. public List<User> getUserList() {
  38. // 处理"/users/"的GET请求,用来获取用户列表
  39. // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
  40. return new ArrayList<>(users.values());
  41. }
  42. @PostMapping
  43. public String postUser(@ModelAttribute User user) {
  44. // 处理"/users/"的POST请求,用来创建User
  45. // 除了@ModelAttribute绑定参数之外,还可以通过@RequestParam从页面中传递参数
  46. users.put(user.getId(), user);
  47. return "success";
  48. }
  49. @GetMapping("{id}")
  50. public User getUser(@PathVariable Long id) {
  51. // 处理"/users/{id}"的GET请求,用来获取url中id值的User信息
  52. // url中的id可通过@PathVariable绑定到函数的参数中
  53. return users.get(id);
  54. }
  55. @RequestMapping(value = "{id}", method = {RequestMethod.PUT, RequestMethod.PATCH})
  56. public String putUser(@PathVariable Long id, @ModelAttribute User user, @RequestParam String param, @RequestBody Map<String, String> body) {
  57. log.info("PUT / PATCH, {}, {}, {}", id, param, body);
  58. // 处理"/users/{id}"的PUT请求,用来更新User信息
  59. User u = users.get(id);
  60. u.setName(user.getName());
  61. u.setAge(user.getAge());
  62. users.put(id, u);
  63. return "success";
  64. }
  65. @DeleteMapping("{id}")
  66. public String deleteUser(@PathVariable Long id, @RequestParam String param, @RequestBody Map<String, String> body) {
  67. log.info("delete, {}, {}, {}", id, param, body);
  68. // 处理"/users/{id}"的DELETE请求,用来删除User
  69. users.remove(id);
  70. return "success";
  71. }
  72. /**
  73. * test
  74. */
  75. @GetMapping("test")
  76. McR<T> test() {
  77. return McR.success();
  78. }
  79. }