英文:
Springfox creates 'ref' type instead of java.io.File object in Swagger 2.0 JSON Definition
问题
@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();
"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"
}
],
For reference, I am using Springfox 2.9.2 and SpringMVC 5.2.2.
<details>
<summary>英文:</summary>
I am attempting to describe a REST POST endpoint that takes two ```java.io.File``` objects as part of a ```multipart/form-data``` payload using Java annotations from Swagger. Upon intitial research, I found that this could be accomplished by specifying implicit parameters with the following properties set (namely ```type```, ```dataType```, and ```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();
In the part of the JSON contract that describes these parameters, everything looks correct **EXCEPT** the ```type``` field value is ```ref``` instead of the excepected ```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"
}
],
I have tried several different combinations of using ```dataType``` vs ```dataTypeClass``` and other strategies from in [this](https://stackoverflow.com/q/51098932/2096689) SO question, but I have not been able to get the JSON contract generated correctly.
For reference, I am using Springfox 2.9.2 and SpringMVC 5.2.2.
</details>
# 答案1
**得分**: 1
经过进一步的搜索,我发现```@ApiImplicitParam```参数的```dataType```不应该是```java.io.File```对象的完全限定路径,而应该指定为```__file```。
[这里](https://springfox.github.io/springfox/docs/current)是SpringFox文档的链接(列在6.5覆盖属性数据类型下)。
<details>
<summary>英文:</summary>
After digging around some more, I was able to find out that ```dataType``` for ```@ApiImplicitParam``` parameters should not be the fully qualified path to the ```java.io.File``` object, it should be specified as ```__file```.
[Here](https://springfox.github.io/springfox/docs/current) is a link to the SpringFox documentation. (Listed under 6.5 Overriding property datatypes)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论