今天开发的时候, 为了验证数据, 想使用Postman批量插入多条数据到服务器中, 但是一直没搜索到使用方法.
服务器的接口如下:
@RequestMapping("uploadFile")
public void uploadFile(@RequestPart("file") MultipartFile file, @RequestPart("jsonObject") JsonObject jsonObject) {
// ...
}
尝试了很多办法, 最后通过下面的办法搞定. 有同样需求的同学可以参考下.
设置的要点是:
1) Content-Type需要设置成undefined.
2) Body中的JsonObject也需要通过文件的方式上传, 并且key的值和API的@RequestPart里的value一致.
这里想强调的是, 大家都知道MultipartFile file参数, 是通过在postman使用file上传的. 但是和file一并通过@RequestPart的Json对象, 也是通过file的方式上传的, 其他方式都不行.
文末附@RequestParam和@RequestPart的区别.
header设置:
body设置
转载: 权威分析@RequestParam和@RequestPart 的区别(官方文档)
https://yq.aliyun.com/articles/614605
@RequestPart
/**
* 单文件上传
* @param file
* @param bucket
* @return
*/
@RequestMapping("uploadFile")
public JsonResult uploadFile(@RequestPart("file") MultipartFile file, @RequestParam String bucket){
String fileUrl = aliossService.uploadFile(file, bucket);
Map<String,String> result = new HashMap<>();
result.put("fileUrl",fileUrl);
return success(result);
}
@RequestParam
/**
* 上传字符串
* @param stringFile
* @param bucket
* @return
*/
@RequestMapping("uploadStringFile")
public JsonResult uploadStringFile(@RequestParam("stringFile") String stringFile, @RequestParam("bucket") String bucket){
String fileUrl = aliossService.uploadStringFile(stringFile, bucket);
Map<String,String> result = new HashMap<>();
result.put("fileUrl",fileUrl);
return success(result);
}
二、比较一下他们的区别吧
@RequestPart
译文:
1.@RequestPart这个注解用在multipart/form-data表单提交请求的方法上。
2.支持的请求方法的方式MultipartFile,属于Spring的MultipartResolver类。这个请求是通过http协议传输的。
3.@RequestParam也同样支持multipart/form-data请求。
4.他们最大的不同是,当请求方法的请求参数类型不再是String类型的时候。
5.@RequestParam适用于name-valueString类型的请求域,@RequestPart适用于复杂的请求域(像JSON,XML)。