一、准备基础环境

  • 创建SysUserController

    1. /**
    2. * (SysUser)表控制层
    3. */
    4. @RestController
    5. @RequestMapping("sysUser")
    6. @RequiredArgsConstructor
    7. public class SysUserController {
    8. private final SysUserService sysUserService;
    9. /**
    10. * 通过id查询
    11. *
    12. * @param id 主键
    13. * @return 单条数据
    14. */
    15. @GetMapping("{id}")
    16. public SysUser getById(@PathVariable Serializable id) {
    17. return this.sysUserService.getById(id);
    18. }
    19. /**
    20. * 新增数据
    21. *
    22. * @param sysUser 实体对象
    23. * @return 新增结果
    24. */
    25. @PostMapping
    26. public boolean insert(@RequestBody SysUser sysUser) {
    27. return this.sysUserService.save(sysUser);
    28. }
    29. }
  • 创建SysUser实体类对象

    1. /**
    2. * (SysUser)表实体类
    3. */
    4. @Data
    5. public class SysUser implements Serializable {
    6. /*主键ID*/
    7. private Long id;
    8. /*姓名*/
    9. private String name;
    10. /*年龄*/
    11. private Integer age;
    12. /*邮箱*/
    13. private String email;
    14. }
  • 创建sys_user数据表

    1. mysql> select * from sys_user;
    2. +-----+--------+-----+--------------------+
    3. | id | name | age | email |
    4. +-----+--------+-----+--------------------+
    5. | 1 | Jone | 18 | test1@baomidou.com |
    6. | 2 | Jack | 20 | test2@baomidou.com |
    7. | 3 | Tom | 28 | test3@baomidou.com |
    8. | 4 | Sandy | 21 | test4@baomidou.com |
    9. | 5 | Billie | 24 | test5@baomidou.com |
    10. +-----+--------+-----+--------------------+
    11. 6 rows in set (0.02 sec)

二、使用crul命令

  • 使用命令,向SysUserController#getById()方法,发送GET请求:
    1. curl http://localhost:8080/sysUser/1
    1. /* 通过id查询 */
    2. @GetMapping("{id}")
    3. public SysUser getById(@PathVariable Serializable id) {
    4. return this.sysUserService.getById(id);
    5. }
    1. 11475@greamrod-pc-company MINGW64 ~/Desktop/curl
    2. $ curl http://localhost:8080/sysUser/1
    3. % Total % Received % Xferd Average Speed Time Time Time Current
    4. Dload Upload Total Spent Left Speed
    5. 100 60 0 60 0 0 7500 0 --:--:-- --:--:-- --:--:-- 7500
    6. {"id":1,"name":"Jone","age":18,"email":"test1@baomidou.com"}
  • 发送POST请求,并携带JSON数据,来新增一条数据记录:
    1. curl -X POST "http://localhost:8080/sysUser" \
    2. -d "{\"id\":200,\"name\":\"Jone\",\"age\":18,\"email\":\"Jone@baomidou.com\"}" \
    3. -H "Accept:*/*" \
    4. -H "Content-Type:application/json"
    1. /* 新增数据 */
    2. @PostMapping
    3. public boolean insert(@RequestBody SysUser sysUser) {
    4. return this.sysUserService.save(sysUser);
    5. }
    1. 11475@greamrod-pc-company MINGW64 ~/Desktop/curl
    2. $ curl -X POST "http://localhost:8080/sysUser" \
    3. > -d "{\"id\":200,\"name\":\"Jone\",\"age\":18,\"email\":\"Jone@baomidou.com\"}" \
    4. > -H "Accept:*/*" \
    5. > -H "Content-Type:application/json"
    6. % Total % Received % Xferd Average Speed Time Time Time Current
    7. Dload Upload Total Spent Left Speed
    8. 100 65 0 4 100 61 363 5545 --:--:-- --:--:-- --:--:-- 6500
    9. true
  • curl目录下,创建一个person.json文件,内容如下: ```bash 11475@greamrod-pc-company MINGW64 ~/Desktop/curl $ pwd /c/Users/11475/Desktop/curl

11475@greamrod-pc-company MINGW64 ~/Desktop/curl $ ls -l total 1 -rw-r—r— 1 11475 197609 83 6月 8 15:13 person.json

11475@greamrod-pc-company MINGW64 ~/Desktop/curl $ cat person.json { “id”:200, “name”: “Jone”, “age”: 18, “email”: “test1@baomidou.com” }

  1. - 发送`POST`请求,并携带一个`person.json`文件,来新增一条数据记录:<br />
  2. > 注意:发送命令时,将目录切换到`person.json`文件所在的目录
  3. ```bash
  4. curl -v -X POST -d @person.json http://localhost:8080/sysUser \
  5. --header "Accept: application/json, application/*+json" \
  6. --header "Content-Type:application/json"
  1. /* 新增数据 */
  2. @PostMapping
  3. public boolean insert(@RequestBody SysUser sysUser) {
  4. return this.sysUserService.save(sysUser);
  5. }
  1. 11475@greamrod-pc-company MINGW64 ~/Desktop/curl
  2. $ curl -v -X POST -d @person.json http://localhost:8080/sysUser \
  3. > --header "Accept: application/json, application/*+json" \
  4. > --header "Content-Type:application/json"
  5. Note: Unnecessary use of -X or --request, POST is already inferred.
  6. % Total % Received % Xferd Average Speed Time Time Time Current
  7. Dload Upload Total Spent Left Speed
  8. 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying ::1:8080...
  9. * Connected to localhost (::1) port 8080 (#0)
  10. > POST /sysUser HTTP/1.1
  11. > Host: localhost:8080
  12. > User-Agent: curl/7.70.0
  13. > Accept: application/json, application/*+json
  14. > Content-Type:application/json
  15. > Content-Length: 73
  16. >
  17. } [73 bytes data]
  18. * upload completely sent off: 73 out of 73 bytes
  19. * Mark bundle as not supporting multiuse
  20. < HTTP/1.1 200
  21. < Content-Type: application/json
  22. < Transfer-Encoding: chunked
  23. < Date: Tue, 08 Jun 2021 08:00:10 GMT
  24. <
  25. { [9 bytes data]
  26. 100 77 0 4 100 73 250 4562 --:--:-- --:--:-- --:--:-- 4812true
  27. * Connection #0 to host localhost left intact
  • 发送GET请求,并携带参数,来新增一条数据记录:
    1. curl -X GET "http://localhost:8080/sysUser?age=30&email=greamrod@qq.com&id=1001&name=greamrod-lost" \
    2. -H "Accept:*/*" \
    3. -H "Content-Type:application/x-www-form-urlencoded"
    1. /* 新增数据 */
    2. @GetMapping
    3. public boolean insert(Long id, String name, Integer age, String email) {
    4. SysUser sysUser = new SysUser(id, name, age, email);
    5. return this.sysUserService.save(sysUser);
    6. }
    1. 11475@greamrod-pc-company MINGW64 ~/Desktop/curl
    2. $ curl -X GET "http://localhost:8080/sysUser?age=30&email=greamrod@qq.com&id=1001&name=greamrod-lost" \
    3. > -H "Accept:*/*" \
    4. > -H "Content-Type:application/x-www-form-urlencoded"
    5. % Total % Received % Xferd Average Speed Time Time Time Current
    6. Dload Upload Total Spent Left Speed
    7. 100 4 0 4 0 0 222 0 --:--:-- --:--:-- --:--:-- 235\
    8. true
  • 发送POST请求,并携带参数,来新增一条数据记录:
    1. curl -X POST "http://localhost:8080/sysUser" \
    2. --data "age=20&email=java@qq.com&id=2001&name=java" \
    3. -H "Accept:*/*" \
    4. -H "Content-Type:application/x-www-form-urlencoded"
    1. /* 新增数据 */
    2. @PostMapping
    3. public boolean insert(@RequestParam("id") Long id, @RequestParam("name") String name, @RequestParam("age") Integer age,
    4. @RequestParam(value = "email", required = false) String email) {
    5. SysUser sysUser = new SysUser(id, name, age, email);
    6. return this.sysUserService.save(sysUser);
    7. }
    1. 11475@greamrod-pc-company MINGW64 ~/Desktop/curl
    2. $ curl -X POST "http://localhost:8080/sysUser" \
    3. > --data "age=20&email=java@qq.com&id=2001&name=java" \
    4. > -H "Accept:*/*" \
    5. > -H "Content-Type:application/x-www-form-urlencoded"
    6. % Total % Received % Xferd Average Speed Time Time Time Current
    7. Dload Upload Total Spent Left Speed
    8. 100 46 0 4 100 42 0 8 0:00:05 0:00:05 --:--:-- 0
    9. true