Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 590
Dan Avatar asked Dec 10 '22 14:12

Dan


2 Answers

Regarding breaking changes in Springfox v2.7.0:

You need to use dataType = "__file" instead of file as commented in https://github.com/springfox/springfox/issues/1285

like image 60
Stefan Hüttemann Avatar answered Dec 13 '22 04:12

Stefan Hüttemann


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.

like image 33
Dan Avatar answered Dec 13 '22 04:12

Dan