【发布时间】:2020-08-19 14:34:09
【问题描述】:
我试图描述一个 REST POST 端点,它使用来自 Swagger 的 Java 注释将两个 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 合同部分,一切看起来都是正确的EXCEPTtype 字段值是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"
}
],
我在this SO 问题中尝试了几种不同的使用dataType 与dataTypeClass 和其他策略的组合,但我无法正确生成JSON 合约。
作为参考,我使用的是 Springfox 2.9.2 和 SpringMVC 5.2.2。
【问题讨论】:
标签: java rest swagger java-io springfox