Forum Discussion

xbentu's avatar
xbentu
New Contributor
5 years ago

I'm trying to add an upload file button in Swagger-UI using annotations with swagger-jaxrs2

Can someone please provide example code on how to add a upload file button in the swagger-ui with annotations using swagger-jaxrs2 library?  I've tried many things to no success.  

This is one of the formats I tried: 

 

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@Path("/upload")
@Operation(
    summary = "summary",
    description = "description"
    responses = {@ApiResponse(
        responseCode = "200", 
        description = "OK"            
    )}
)
public Response uploadFile(
@FormDataParam("file") InputStream inputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail
)throws RestException{
    //process file code
}


This is the request body that gets produced by swagger-jaxrs2 for file upload with the above code: 

      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  $ref: '#/components/schemas/FormDataContentDisposition'

 

  • xbentu's avatar
    xbentu
    New Contributor

    I finally found the solution after 3 weeks of swapping Annotation combinations.  

    @Parameter(schema = @Schema(name = "file", type = "string", format = "binary"))

    This is needed for the InputStream @FormDataParam annotation.

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/upload")
    @Operation(
        summary = "summary",
        description = "description"
        responses = {@ApiResponse(
            responseCode = "200", 
            description = "OK"            
        )}
    )
    public Response uploadFile(
    @Parameter(name = "file", type = "string", format = "binary")
    @FormDataParam("file") InputStream inputStream,
    @FormDataParam("file") FormDataContentDisposition fileDetail
    )throws RestException{
        //process file code
    }