如何在 Swagger-ui 中为正文输入流生成文件选择器
How to generate file chooser in Swagger-ui for body inpustream
您好,我有以下 jaxrs 条目
@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();
}
}
以及为该条目生成的 swagger-ui 页面
我想知道如何记录我的参数,以便在 swagger 中仅获取一个作为文件选择器显示的参数-ui。
我对 Swagger 不是很熟悉 UI,但是这个帖子可能会有帮助
https://github.com/swagger-api/swagger-ui/issues/72
我看到一个使用@ApiParam
注释的示例
和这个 post 谈论文件上传与
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()
}
@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();
}
我想你需要在下面的参数描述中使用:
@RequestPart("file") MultipartFile file
至少对我来说,它提供了一个带有文件选择按钮的 swagger 表单。
在此处的示例中找到了解决方案:
https://github.com/springfox/springfox-demos
您好,我有以下 jaxrs 条目
@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();
}
}
以及为该条目生成的 swagger-ui 页面
我想知道如何记录我的参数,以便在 swagger 中仅获取一个作为文件选择器显示的参数-ui。
我对 Swagger 不是很熟悉 UI,但是这个帖子可能会有帮助
https://github.com/swagger-api/swagger-ui/issues/72
我看到一个使用@ApiParam
注释的示例
和这个 post 谈论文件上传与
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()
}
@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();
}
我想你需要在下面的参数描述中使用:
@RequestPart("file") MultipartFile file
至少对我来说,它提供了一个带有文件选择按钮的 swagger 表单。
在此处的示例中找到了解决方案: https://github.com/springfox/springfox-demos