Java 用于流式分段文件上传的 Swagger (Springfox) 注释

Java Swagger (Springfox) annotations for streaming multipart file upload

我们正在使用 spring 个控制器来处理文件上传:

例如:

@RequestMapping(value = "/scan", method = RequestMethod.POST, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public ScanResult scan(HttpServletRequest request) throws IOException, FileUploadException {
    return scanService.scanFile(parseMultipart(request));
}

但我们没有使用任何多部分解析器,我们正在从 servlet 请求输入流中流式传输文件。出于性能原因,我们需要立即开始处理文件。

当这样做时,我们似乎无法对多部分文件使用典型的 detection/configuration。我知道 Springfox(我们用它来生成 swagger 文档)如果将 MultipartFile 视为控制器参数,将生成适当的 swagger 控件,但我们不会遇到这种情况。

是否有任何其他配置选项可用于向 springfox 提示我们要在此处上传文件?

在这里找到我的答案:https://github.com/springfox/springfox/issues/1285

下面的隐式参数给了我我需要的东西:

@ApiImplicitParams (value = {
    @ApiImplicitParam(dataType = "file", name = "file", required = true,paramType = "form")}
@RequestMapping(value = "/scan", method = RequestMethod.POST, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public ScanResult scan(HttpServletRequest request) throws IOException, FileUploadException {
    return scanService.scanFile(parseMultipart(request));
}

这会向 API 添加一个简单的文件选择器。更令人困惑的是,这个功能在我使用的 Springfox 2.4 版本中被破坏了。添加注释和更新版本就是我需要做的。

关于 Springfox v2.7.0 中的重大更改:

您需要使用 dataType = "__file" 而不是 https://github.com/springfox/springfox/issues/1285

中评论的 file