【发布时间】:2019-07-04 02:35:27
【问题描述】:
我正在尝试记录一个 API 方法,它将接收一个文件和两个参数作为 int。使用 swagger 编辑器,我能够描述我想要的内容,但无法使用注释复制它。
这是我在 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,我会上传文件。
@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);
}
}
);
}
提前致谢。
【问题讨论】:
-
你试过
consumes = MediaType.MULTIPART_FORM_DATA_VALUE吗? -
我正在使用 micronaut,但这个常量不可用。但至于我检查这只是说“多部分/表单数据”的另一种方式,对吗?