目的
使用 RestClient 对SpringBoot 开发的Rest接口测试。以掌握RestClient 的使用。
实践
接口前缀为: /rest-client, Controller 方法:
@RestController @RequestMapping("/rest-client") public class RestClientController { private Map buildRestMap() { return new HashMap(); } }
GET 方法
该接口设置了一个请求参数 name 和 一个路径参数 id。
接口:
@GetMapping("/get/{id}") public Map getTest(@RequestParam String name, @PathVariable("id") Integer id) { Map 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 headers(HttpServletRequest request) { Map resultMap = buildRestMap(); Enumeration 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 putTest(@RequestParam String name) { Map 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 deleteTest(@RequestParam String name) { Map 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 postTest(@RequestBody User user) { Map 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 upload(@RequestParam("file") MultipartFile file) { Map 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:tmptmp01.txt --WebAppBoundary--
结果:
总结
文章演示了在VSCode中使用RestClient对SpringBoot开发的Rest接口的测试,包含GET、PUT、DELETE、POST(包含表单提交和文件上传),并演示了如何传递路径参数、请求参数、请求体参数、请求头信息等。
演示虽然简单,但也给出了基本使用方法,在工作中可以使用这些简单的语法构造出更复杂的请求方法。
项目代码