spring-boot - OpenAPI 3.0 响应中的 MultipartFile 问题
问题描述
我正在实现一个带有 GET 的 REST 接口,它应该返回一个 Multipart 文件。但是通过 OPEN API 可用的所有内容是
responses:
200:
description: Requested Document
content:
application/pdf:
schema:
type: string
format: binary
image/png:
schema:
type: string
format: binary
multipart/form-data:
schema:
type: string
format: binary
当我通过 OPEN API 生成器 plgin 3.3.4 构建时,这将转换为下面
@ApiOperation(value = "Returns a Document object for the given groupId", nickname = "getDocumentForDocumentId", notes = "Returns all Document objects for the given groupId", response = Resource.class, tags={ "DocumentUpload", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Requested Document", response = Resource.class),
@ApiResponse(code = 400, message = "Invalid Request"),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden"),
@ApiResponse(code = 404, message = "Not Found"),
@ApiResponse(code = 500, message = "Internal Server Error") })
@RequestMapping(value = "/documentupload/v1/{groupId}",
produces = { "application/pdf", "image/png", "multipart/form-data" },
method = RequestMethod.GET)
default ResponseEntity<Resource> getDocumentForDocumentId(@ApiParam(value = "The Unique ID to search your documents",required=true) @PathVariable("groupId") String groupId,@NotNull @ApiParam(value = "", required = true) @Valid @RequestParam(value = "documentId", required = true) String documentId,@ApiParam(value = "" ,required=true) @RequestHeader(value="sourceSystemId", required=true) String sourceSystemId,@ApiParam(value = "") @Valid @RequestParam(value = "objectName", required = false) String objectName) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
如果你看到我得到 response = Resource.class 并产生部分显示产生 = { "application/pdf", "image/png", "multipart/form-data" }
所以我只能发送资源字节流而不是 Multipart(info+binary)
问题:有没有办法配置 open api yml 以便我在生成的控制器类中获得 Multipart 作为响应类?
如果我尝试使用 response = CommonsMultipartFile.class 手动编写函数并删除产生 = { "application/pdf", "image/png", "multipart/form-data" } 我收到以下异常
{"timestamp":"2020-03-12T12:46:22.104+0000","status":200,"error":"OK","message":"Type definition error: [simple type, class java.io.FileDescriptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.web.multipart.commons.CommonsMultipartFile[\"fileItem\"]->org.apache.commons.fileupload.disk.DiskFileItem[\"inputStream\"]->java.io.FileInputStream[\"fd\"])","path":"/document-upload/documentupload/v2/u7677"}
我使用下面的代码将来自后端的 AWS S3 对象转换为 Multipartfile,然后将其作为 REST API 的响应发送
S3ObjectInputStream objectInputStream = s3Object.getObjectContent();
FileItem fileItem = new DiskFileItemFactory().createItem("file",
s3Object.getObjectMetadata().getContentType(), false, fileName);
try (InputStream in = objectInputStream; OutputStream out = fileItem.getOutputStream()) {
in.transferTo(out);
} catch (Exception e) {
throw new IllegalArgumentException("Invalid file: " + e, e);
}
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
解决方案
推荐阅读
- javascript - 如何从js将原始打印代码发送到ip打印机
- python - leetcode问题中没有定义NameError
- css - 如何使用 CSS 为使用 python-dash 创建的应用程序解析我的 Web 应用程序格式
- python - Python:安装geopandas时出错
- javascript - 如何使用字典根据 jQuery 中的另一个选择过滤选择选项?
- css - 使用 CSS 动态替换文本
- javascript - 获取 ReferenceError:在传递 contextIsolation 时未定义要求:带有预加载脚本的 true
- jquery - Wordpress:猫头鹰轮播 jQuery 插件和 Woocommerce 产品图片问题
- javascript - 编剧自动滚动到无限滚动页面底部
- r - R:如何将绘图/图形导出为 png 或 pdf 文件