Springfox在Swagger 2.0 JSON定义中创建’ref’类型,而不是java.io.File对象。

huangapple go评论76阅读模式
英文:

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=&quot;controlFile&quot;,
			value=&quot;Control file to be used in the comparison.&quot;,
			required=true,
			type=&quot;file&quot;,
			paramType=&quot;form&quot;,
			dataType=&quot;java.io.File&quot;),
	@ApiImplicitParam(
			name=&quot;testFile&quot;,
			value=&quot;Test file to be used in the comparison.&quot;,
			required=true,
			type=&quot;file&quot;,
			paramType=&quot;form&quot;,
			dataType=&quot;java.io.File&quot;)
})
@PostMapping(
		consumes=MediaType.MULTIPART_FORM_DATA,
		produces=MediaType.APPLICATION_JSON
)
public ResponseEntity&lt;Void&gt; 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>



huangapple
  • 本文由 发表于 2020年5月5日 10:08:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61604557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定