1

我有以下招摇的 yaml 文件

/tasks/{id}/documents/files/data:
    post:
      tags:
        - task-documents
      summary: Upload document
      description: Upload document
      operationId: uploadDocument
      parameters:
        - name: id
          in: path
          description: ID
          required: true
          schema:
            type: string
      requestBody:
        content:
          multipart/form-data:
            schema:
              required:
                - json
                - upfile
              type: object
              properties:
                upfile:
                  type: string
                  description: Document Content InputStream
                  format: binary
                json:
                  description: Document Metadata
                  $ref: "#/components/schemas/UploadRequest"
        required: true
      responses:
        200:
          description: successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Document'
        401:
          description: Unauthorized
          content: {}
        500:
          description: Internal Server Error
          content: {}
components:
  schemas:
     UploadRequest:
      type: object
      properties:
        documentName:
          type: string
          description: File Name of Document
        contentType:
          type: string
          description: Mime type of document file
        description:
          type: string
          description: Description for document
      required:
        - contentType

我的 build.gradle 文件有以下生成代码的条目。

// Swagger
    compile "io.swagger.codegen.v3:swagger-codegen:${versions.swaggerCodegenVersion}"
    swaggerCodegen "io.swagger.codegen.v3:swagger-codegen-cli:${versions.swaggerCodegenVersion}"
    implementation "io.swagger:swagger-annotations:${versions.swaggerVersion}"
    implementation group: 'io.swagger.codegen.v3', name: 'swagger-codegen-generators', version: '1.0.23'

swagger-codegen-v3 在我的 API 类中生成以下方法。

    @POST
    @Path("/{id}/documents/files/data")
    @Consumes({ "multipart/form-data" })
    @Produces({ "application/json" })
    @ApiOperation(value = "Upload document", notes = "Upload document", response = Document.class, authorizations = {
        @Authorization(value = "cut_basic"),
        @Authorization(value = "cut_oauth", scopes = {
        @AuthorizationScope(scope = "", description = "")})}, tags={ "documents", })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "successful operation", response = Document.class),
        @ApiResponse(code = 401, message = "Unauthorized", response = Document.class), 
        @ApiResponse(code = 500, message = "Internal Server Error", response = Document.class)})
    public Response uploadDocument(@Context UriInfo uriInfo, @FormDataParam("upfile") InputStream upfileInputStream, @FormDataParam("upfile") FormDataContentDisposition upfileDetail
,@Parameter(description = "", required=true)@FormDataParam("json")  UploadRequest json
,@Parameter(description = "ID",required=true) @PathParam("id") String id, @Context SecurityContext securityContext) throws NotFoundException {

        LOG.debug("Upload document : Upload document");
        LOG.debug( "upfile :" + upfile + ", " +  "json :" + json + ", " +  "id :" + id + ", " +  ";");
        delegate.setProviders(providers);
        Response response = delegate.uploadDocument(uriInfo,upfileInputStream, upfileDetail,json,id,securityContext);

        LOG.debug("Exiting - uploadDocument");
        return response;
    }

当我尝试点击请求时,我总是将 UploadRequest 对象设为 null,尽管我以以下格式传递值。但是,我正确填充了 FormDataContentDisposition 和 InputStream。

--Boundary_123456789_123456789
Content_transfer-Encoding: binary
Content-Disposition: form-data; name="upfile"; filename="test.txt"

I am a test file!!!

--Boundary_123456789_123456789
Content-Disposition: form-data; name="json"
Content-Type: application/json
{
  "documentName": "string",
  "contentType": "string",
  "description": "string"
}

--Boundary_123456789_123456789--

不太清楚里面发生了什么。有人可以帮我解释为什么 json 对象会为空。我在这里错过了什么吗?

4

0 回答 0