2017-06-29 57 views
0

你好,我有以下JAXRS進入 如何產生揚鞭的UI文件選擇身體inpustream

@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(); 
    } 
} 

而產生招搖的UI頁面找到相關enter image description here

我想知道如何在swagger-ui中獲取作爲文件選擇器呈現的參數的文件。

回答

0

@sdc:您說得對,我必須使用多部分表單數據才能在Swagger-ui中獲取工作文件選擇器。我也不得不使用@ApiImplicitParam來使它工作。

@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(); 
} 
0

我不是很熟悉揚鞭UI,但

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

我看到使用@ApiParam註釋

的信息以及與文件上傳這篇文章談判爲例這個線程可能會有所幫助 def uploadFile註釋

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() 
    } 
+0

您好,非常感謝您的回答,我想 '公開回應testput( @ApiParam(值= 「pwet」,類型= 「文件」,格式= 「二進制」,要求= TRUE)證書的InputStream )' 現在只有一個參數,但仍然沒有文件選擇器:/ 注意:我在回答中編輯了示例 – mlapeyre

+0

你是什麼意思你希望參數作爲文件選擇器來呈現?你的意思是'testput'方法的輸入類型應該是'JFileChooser'對象嗎? [JFileChooser JavaDocs](https://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html) - 我添加了另一個鏈接到我的回答上面 – sdc

+0

我不想要一個Swing組件在我的api XD 我想在swagger-ui生成的頁面中的網頁文件選擇器:) 我沒有興趣使用多部分形式的數據,八位字節流似乎足夠我.. – mlapeyre