Search code examples
javaswaggerspringfox

Java Swagger (Springfox) annotations for streaming multipart file upload


We are using spring controllers to handle file uploads:

For example:

@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));
}

But we are not using any multipart resolver, we are streaming the files from the servlet request input stream. We need to start processing the file immediately for performance reasons.

When doing this this way, we can't seem to use the typical detection/configuration for multipart files. I know Springfox (which we use to generate our swagger docs) will generate the appropriate swagger controls if it sees a MultipartFile as a controller parameter, which will not be the case for us.

Are there any other config options available to hint to springfox that we want a file upload here?


Solution

  • Found my answer here: https://github.com/springfox/springfox/issues/1285

    The following implicit params give me what I need:

    @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));
    }
    

    This adds a simple file picker to the API. To make things more confusing, turns out this functionality was broken in Springfox 2.4 - the version I was using. Adding that annotation and updating versions was all I needed to do.