1

Hello i have the following jaxrs entry

@PUT()
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
@ApiOperation(value = "Bla bla.")
@Path("secure/flappy")
public Response testput(
        @ApiParam(value = "pwet",type = "file",format = "binary", required = true) InputStream certificate) throws Throwable {
    try (InputStream stream = certificate) {
        //Consume stream
        return Response.ok().build();
    }
}

And the generated swagger-ui page for this entryenter image description here

I would like to know how to document my paremeter for getting only one parameter presented as a file chooser in swagger-ui.

mlapeyre
  • 202
  • 3
  • 8

3 Answers3

1

@sdc: You're right i had to use multi part form data for getting a working file chooser in Swagger-ui. I also had to use @ApiImplicitParam for getting it working.

@PUT()
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@ApiOperation(value = "Bla bla.")
@Path("secure/flappy")
@ApiImplicitParams({
    @ApiImplicitParam(name = "file", value = "bla bla.", required = true, dataType = "java.io.File", paramType = "form")
})
public Response testput(@ApiParam(hidden = true) @FormDataParam("file") final  InputStream certificate
) throws Throwable {
    //TODO do something
    return Response.ok().build();
}
mlapeyre
  • 202
  • 3
  • 8
0

I am not very familiar with Swagger UI, but this thread might be helpful

https://github.com/swagger-api/swagger-ui/issues/72

I see an example using an@ApiParam annotation

and this post talks about a file upload with the def uploadFile annotation

https://github.com/swagger-api/swagger-ui/issues/169

  @PUT
  @Path("/secure/flappy")
  @Consumes(Array(MediaType.MULTIPART_FORM_DATA))
  @ApiOperation(value = "test a put file upload")
  def uploadFile(
    @ApiParam(value = "file to upload", required=false) @FormDataParam("file") inputStream: InputStream,
    @ApiParam(value = "file detail") @FormDataParam("file") fileDetail: FormDataContentDisposition) = {
    val uploadedFileLocation = "./" + fileDetail.getFileName
    IOUtils.copy(inputStream, new FileOutputStream(uploadedFileLocation))
    val msg = "File uploaded to " + uploadedFileLocation + ", " + (new java.io.File(uploadedFileLocation)).length + " bytes"
    val output = new com.wordnik.swagger.sample.model.ApiResponse(200, msg)
    Response.status(200).entity(output).build()
  }
sdc
  • 2,603
  • 1
  • 27
  • 40
  • hello, thank you for your answer, i tried `public Response testput( @ApiParam(value = "pwet",type = "file",format = "binary", required = true) InputStream certificate)` Now there is only one parameter but still no file chooser :/ Note: i edited the example in the answer – mlapeyre Jun 29 '17 at 19:18
  • What do you mean by you want the parameter to be presented as a file chooser? Do you mean that the input type to the `testput` method should be a `JFileChooser` object? [JFileChooser JavaDocs](https://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html) -- I added another link to my answer above – sdc Jun 29 '17 at 20:48
  • I don't want a Swing component in my api XD I would like a web file chooser in the page generated by swagger-ui :) I have no interest to use Multi part form data, octet stream seems sufficient for me.. – mlapeyre Jun 29 '17 at 21:35
  • Did my answer get you what you needed? – sdc Jun 30 '17 at 04:44
  • 1
    If you are looking for web file chooser, the code I added should do it. As far as I know, `"multipart/form-data"` is required when doing a fileupload via a form given that the file size might be bigger than the allowed request size - see this SO multipart-form [post](https://stackoverflow.com/questions/1342506/why-is-form-enctype-multipart-form-data-required-when-uploading-a-file) – sdc Jun 30 '17 at 15:02
0

I suppose you need to use in the parameter description following:

@RequestPart("file") MultipartFile file

At least for me it gives a swagger form with a file selection button.

enter image description here

The solution was found in the examples here: https://github.com/springfox/springfox-demos

Mureinik
  • 297,002
  • 52
  • 306
  • 350