Java Swagger(Springfox)批注,用于流式传输多部分文件

Dan*_*Dan 3 java swagger springfox

我们正在使用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));
}
Run Code Online (Sandbox Code Playgroud)

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

以这种方式执行操作时,我们似乎无法对多部分文件使用典型的检测/配置。我知道Springfox(我们用于生成我们的swagger文档)会在将MultipartFile作为控制器参数的情况下生成适当的swagger控件,但对我们而言并非如此。

还有其他配置选项可用来提示springfox我们要在此处上传文件吗?

Ste*_*ann 6

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

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


Dan*_*Dan 5

在这里找到我的答案: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));
}
Run Code Online (Sandbox Code Playgroud)

这将向API添加一个简单的文件选择器。为了使事情更加混乱,事实证明该功能在Springfox 2.4(我正在使用的版本)中被打破。我只需要添加注释和更新版本。