May
16
Swagger API: 上传文件
踩了个小坑,记录一下。
swagger api 定义:
/upload:
post:
tags:
- "tag"
summary: "summary"
operationId: uploadFile
consumes:
- multipart/form-data
parameters:
- name: "data"
in: "formData"
type: "file"
required: true
description: "file content"
responses:
200:
description: "success"
schema:
$ref: "#/definitions/UploadFileResponse"
生成的 API:
直接用这个 API 请求会报错:
swagger Required request part 'file' is not present
细看发现是 swagger 生成的是 @RequestPart("file") 而不是 @RequestPart("data"),需要手动修改过来才能正确读取到文件字段。
转载请注明出自 ,如是转载文则注明原出处,谢谢:)
RSS订阅地址: https://www.felix021.com/blog/feed.php 。
swagger api 定义:
引用
/upload:
post:
tags:
- "tag"
summary: "summary"
operationId: uploadFile
consumes:
- multipart/form-data
parameters:
- name: "data"
in: "formData"
type: "file"
required: true
description: "file content"
responses:
200:
description: "success"
schema:
$ref: "#/definitions/UploadFileResponse"
生成的 API:
@RequestMapping(value = {"/sf/express/upload_channel_file" }, produces = { "application/json" }, consumes = { "multipart/form-data" }, method = RequestMethod.POST)
default ResponseEntity<UploadFileResponse> uploadFile(@ApiParam(value = "file detail") @Valid @RequestPart("file") MultipartFile data) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
default ResponseEntity<UploadFileResponse> uploadFile(@ApiParam(value = "file detail") @Valid @RequestPart("file") MultipartFile data) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
直接用这个 API 请求会报错:
swagger Required request part 'file' is not present
细看发现是 swagger 生成的是 @RequestPart("file") 而不是 @RequestPart("data"),需要手动修改过来才能正确读取到文件字段。
欢迎扫码关注:
转载请注明出自 ,如是转载文则注明原出处,谢谢:)
RSS订阅地址: https://www.felix021.com/blog/feed.php 。