如上图所示,JavaBean (包含一个 MultipartFile[] file 属性) 作为参数时,不能正确识别 MediaType 为 multipart/form-data
springboot 2.2.7.RELEASE
springdoc-openapi-ui 1.6.9
knife4j-openapi3-spring-boot-starter 4.0.0
knife4j 效果图 (正确识别需要放开注释部分)
swagger 效果图
github issue : https://github.com/xiaoymin/swagger-bootstrap-ui/issues/538
在OpenAPI3里面确实我复测了一下,有这个问题,但是是因为那个array没解析出来(目前版本的ui确实存在问题)
不过我后来升级了springdoc的版本1.6.14(后面Knife4j的版本会迭代到这个),解析出来的OpenAPI规范却不太一样。。
第一种情况是加注解描述的,代码如下:
@Operation(summary = "附件上传",description = "附近上传-xxx",requestBody = @RequestBody(content = {
@Content(mediaType = MediaType.MULTIPART_FORM_DATA_VALUE,schema = @Schema(type = "object"),
schemaProperties = {
@SchemaProperty(name = "file",array = @ArraySchema(schema = @Schema(type = "string",format = "binary",requiredMode = Schema.RequiredMode.REQUIRED))),
@SchemaProperty(name = "module",schema = @Schema(type = "string",required = true)),
@SchemaProperty(name = "bizNo",schema = @Schema(type = "string",required = true)),
@SchemaProperty(name = "bizType",schema = @Schema(type = "integer",required = true))
})
}))
@PostMapping(value = "/module/upload1",consumes = MediaType.MULTIPART_FORM_DATA_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> fileModule1(FileRequestVo fileRequestVo){
return ResponseEntity.ok(RandomUtil.randomString(23));
}
解析的规范:
第二种情况,不加任何注释,代码:
@PostMapping(value = "/module/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> fileModule(FileRequestVo fileRequestVo){
return ResponseEntity.ok(RandomUtil.randomString(23));
}
解析得到的规范:
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
我只能先以这个版本为准了
我在 knife4j-openapi3-spring-boot-starter 4.5.0版本中,
@Operation(summary = "文件上传")
@PostMapping(value = "/comic-factory/v1/file/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public R<FileVO> upload(@Validated FileUploadDTO dto) {
return R.ok(fileService.upload(dto));
}
@Data
@Schema(name = "文件上传参数")
public class FileUploadDTO implements Serializable {
@Schema(title = "名称", example = "1.txt")
@NotBlank(message = "名称不能为空")
@Size(min = 4, max = 128, message = "名称长度必须在4 ~ 128位")
private String name;
@Schema(title = "场景", example = "one-day")
@NotBlank(message = "场景不能为空")
private String scene;
@Schema(description = "有效期(单位秒,-1:永久)", example = "86400")
private Long expiry;
@Schema(title = "文件", format = "binary")
@NotNull(message = "文件不能为空")
private MultipartFile file;
}
仍然出现无法识别文件上传form-data,但是通过swagger-ui显示正确,是哪里配置的不对吗
登录 后才可以发表评论