RestClient 接口测试实践

目的

使用 RestClient 对SpringBoot 开发的Rest接口测试。以掌握RestClient 的使用。

实践

接口前缀为: /rest-client, Controller 方法:

@RestController
@RequestMapping("/rest-client")
public class RestClientController {
    /**
     * 构建空Map
     * 
     * @return
     */
    private Map<String, Object> buildRestMap() {
        return new HashMap<>();
    }
}

GET 方法

该接口设置了一个请求参数 name 和 一个路径参数 id。

接口:

    @GetMapping("/get/{id}")
    public Map<String, Object> getTest(@RequestParam String name, @PathVariable("id") Integer id) {
        Map<String, Object> resultMap = buildRestMap();
        resultMap.put("in", "name : " + name + " , id : " + id);
        resultMap.put("out", "hello : " + name);
        return resultMap;
    }

rest client 请求:

### 
#GET 请求测试 123 为路径参数 id 的值
GET http://localhost:8080/rest-client/get/123?name=小刚同学

结果:
在这里插入图片描述

传递Headers信息

该接口以GET方法为例,其他HTTP请求方法一样。给接口设置为将拿到的请求头信息原样返回,其中Auth-Sec: csdn-12345678899是自定义头信息。
接口:

    @GetMapping("/get/headers")
    public Map<String, Object> headers(HttpServletRequest request) {
        Map<String, Object> resultMap = buildRestMap();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String hName = headerNames.nextElement();
            resultMap.put(hName, request.getHeader(hName));
        }
        return resultMap;
    }

rest client 请求:

### 
#GET 请求头信息
GET http://localhost:8080/rest-client/get/headers
accept: application/json, text/javascript, */*; q=0.01
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6
content-length: 15
content-type: application/json
Auth-Sec: csdn-12345678899

结果:
在这里插入图片描述

PUT 方法

接口:

    @PutMapping("/put")
    public Map<String, Object> putTest(@RequestParam String name) {
        Map<String, Object> resultMap = buildRestMap();
        resultMap.put("method", "PUT");
        resultMap.put("in", name);
        return resultMap;
    }

rest client 请求:

### 
# PUT 请求
PUT http://localhost:8080/rest-client/put?name=小米你好

结果:
在这里插入图片描述

DELETE 方法

接口:

    @DeleteMapping("/del")
    public Map<String, Object> deleteTest(@RequestParam String name) {
        Map<String, Object> resultMap = buildRestMap();
        resultMap.put("msg", "你删除了用户: " + name);
        return resultMap;
    }

rest client 请求:

### 
# DELETE 请求 
DELETE http://localhost:8080/rest-client/del?name=小D

结果:
在这里插入图片描述

POST 请求体

该用例用来演示表单提交

接口:

    @PostMapping("/post/user")
    public Map<String, Object> postTest(@RequestBody User user) {
        Map<String, Object> resultMap = buildRestMap();
        resultMap.put("用户信息", user);
        return resultMap;
    }

rest client 请求:

### 
# POST 请求 提交对象
POST http://localhost:8080/rest-client/post/user
Content-Type: application/json

# 注意要有空行
{
    "name" : "小浦",
    "age" : 12
}

结果:

POST 上传文件

该用例用于演示文件上传
接口:

    @PostMapping("/post/file")
    public Map<String, Object> upload(@RequestParam("file") MultipartFile file) {
        Map<String, Object> resultMap = buildRestMap();
        resultMap.put("msg", "上传文件成功!");
        resultMap.put("fileName", file.getOriginalFilename());
        resultMap.put("fileSize", file.getSize());
        return resultMap;
    }

rest client 请求:

### 
# POST 请求 上传文件
POST http://localhost:8080/rest-client/post/file
# 请求内容类型
Content-Type: multipart/form-data; boundary=WebAppBoundary

# 请求体  注意⚠️:空一行
--WebAppBoundary
# 描述
Content-Disposition: form-data; name="file"; filename="tmp01.txt"

#文件地址 注意⚠️:空一行
< D:\tmp\tmp01.txt
--WebAppBoundary--

结果:
在这里插入图片描述

总结

文章演示了在VSCode中使用RestClient对SpringBoot开发的Rest接口的测试,包含GET、PUT、DELETE、POST(包含表单提交和文件上传),并演示了如何传递路径参数、请求参数、请求体参数、请求头信息等。

演示虽然简单,但也给出了基本使用方法,在工作中可以使用这些简单的语法构造出更复杂的请求方法。

项目代码

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值