我正在尝试使用 Swagger 中的 Java 注释来描述一个 REST POST 端点,该端点将两个 java.io.File
对象作为 multipart/form-data
负载的一部分。经过初步研究,我发现这可以通过指定具有以下属性集的隐式参数(即 type
、dataType
和 paramType
)来完成
@ApiImplicitParams({
@ApiImplicitParam(
name="controlFile",
value="Control file to be used in the comparison.",
required=true,
type="file",
paramType="form",
dataType="java.io.File"),
@ApiImplicitParam(
name="testFile",
value="Test file to be used in the comparison.",
required=true,
type="file",
paramType="form",
dataType="java.io.File")
})
@PostMapping(
consumes=MediaType.MULTIPART_FORM_DATA,
produces=MediaType.APPLICATION_JSON
)
public ResponseEntity<Void> submitComparisonRequest(
final UriComponentsBuilder uriBuilder,
@Context final HttpServletRequest request) {
try {
final FileItemFactory factory = new DiskFileItemFactory();
final ServletFileUpload upload = new ServletFileUpload(factory);
final FileItemIterator items = upload.getItemIterator(request);
final FileItemStream control = items.next();
final FileItemStream test = items.next();
在描述这些参数的 JSON 合约部分中,一切看起来都正确,type
字段值是 ref
而不是预期的java.io.File
.
"parameters": [
{
"name": "controlFile",
"in": "formData",
"description": "Control file to be used in the comparison.",
"required": true,
"type": "ref"
},
{
"name": "testFile",
"in": "formData",
"description": "Test file to be used in the comparison.",
"required": true,
"type": "ref"
}
],
我尝试了使用 dataType
与 dataTypeClass
以及 this 中的其他策略的几种不同组合。所以问题,但我无法正确生成 JSON 合约。
作为引用,我使用的是 Springfox 2.9.2 和 SpringMVC 5.2.2。
最佳答案
经过更多研究后,我发现 @ApiImplicitParam
参数的 dataType
不应该是 java.io 的完全限定路径.File
对象,应指定为 __file
。
Here是 SpringFox 文档的链接。 (在 6.5 重写属性数据类型下列出)
关于java - Springfox 在 Swagger 2.0 JSON 定义中创建 'ref' 类型而不是 java.io.File 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61604557/