如何指定带参数的分段文件上传

时间:2019-07-04 02:35:27

标签: java swagger micronaut

我正在尝试记录一个API方法,该方法将接收一个文件和两个参数作为int。使用swagger编辑器,我能够描述我想要的内容,但是无法使用注释来复制它。

what I want

这就是我在swagger编辑器上绘制的内容

requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                flow:
                  type: integer
                environment:
                  type: integer
                file:
                  type: string
                  format: binary
        required: true

如果我使用consumes = MediaType.MULTIPART_FORM_DATA,则会得到参数。如果我使用consumes = MediaType.APPLICATION_OCTET_STREAM,我将上传文件。

failed attemp 1 failed attemp 2

@Operation(summary = "Unpack Files",
            description = "Receives a packed zip or gzip file with xml files inside or receives xml files",
            security = @SecurityRequirement(name = "apiKey"),
            responses = {
                    @ApiResponse(responseCode = "201", description = "Created"),
                    @ApiResponse(responseCode = "400", description = "Something Went Wrong"),
                    @ApiResponse(responseCode = "401", description = "Unauthorized"),
                    @ApiResponse(responseCode = "503", description = "Service Unavailable")
            },
            requestBody = @RequestBody(
                content = @Content(
                    mediaType = MediaType.MULTIPART_FORM_DATA,
                    schema = @Schema(implementation = Document.class, format = "binary"),
                    encoding = @Encoding(
                            name = "file",
                            contentType = "application/xml, application/zip, application/gzip"
                    )
                ),
                required = true
            )
    )
    @Post(value = "/unpack", consumes = MediaType.APPLICATION_OCTET_STREAM)
    public Single<HttpResponse<String>> upload(StreamingFileUpload file, int flow, int environment) throws IOException {
        return Single.just(new Document(file.getFilename(), environment, flow))
            .flatMap(DocumentValidation::validateDocumentExtension)
            .doOnError(throwable -> {
                log.error("Validation exception: {}", throwable.getMessage());
                exception = throwable.getMessage();
            })
            .doOnSuccess(doc -> {
                log.info("File saved successfuly");
                File tempFile = File.createTempFile(file.getFilename(), "temp");
                file.transferTo(tempFile);
            })
            .map(success -> {
                if (exception != null || !exception.equals("")) {
                    return HttpResponse.<String>status(HttpStatus.CREATED).body("Uploaded");
                } else {
                    return HttpResponse.<String>status(HttpStatus.SERVICE_UNAVAILABLE).body(exception);
                }
            }
        );
    }

谢谢。

1 个答案:

答案 0 :(得分:0)

看起来像缺少@QueryValue

摘自文档6.4 Simple Request Binding

  

来自请求URI变量或请求参数的绑定| @QueryValue字符串myParam

摘自文档6.19 File Uploads

  

该方法设置为使用MULTIPART_FORM_DATA

     

方法参数与表单属性名称匹配。在这种情况下,文件将匹配

     

StreamingFileUpload.transferTo(java.lang.String)方法用于将文件传输到服务器。

Kotlin简单:

@Controller
class SomeController {

    @Post(value = "/", consumes = [MediaType.MULTIPART_FORM_DATA])
    fun upload(file: StreamingFileUpload,
               @QueryValue flow: Int,
               @QueryValue environment: Int): Single<HttpResponse<String>> {
        val tempFile = File.createTempFile(file.filename, "temp")
        return Single.fromPublisher(file.transferTo(tempFile))
                .map { success ->
                    if (success) {
                        HttpResponse.ok("Uploaded");
                    } else {
                        HttpResponse.status<String>(HttpStatus.CONFLICT)
                                .body("Upload Failed")
                    }
                }
    }

}