首页 > 解决方案 > 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);

标签: spring-bootopenapiopenapi-generator

解决方案


推荐阅读