Is it any ability to upload MultipartFile with Model in Swagger? (In comment 415 Unsupported Media Type Resolved)

人盡茶涼 提交于 2021-01-16 03:52:42

问题


I'm using the last (and only)

 <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
 </dependency>

with spring boot

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.1</version>
    <relativePath/> <!-- lookup parent from repository -->
 </parent>

I would like to have ability to upload file and model in one controller, so I created one: image

in this case on swagger-ui I see only file uploading without model: image

I tried this case in postman and it works fine image

Is it a bug? Or should I add some other annotations to make it work?


回答1:


The problem was in Configuration class, in Docket Bean. Here I'm defining route to my Controllers to exclude others from swagger-ui. The problem was in DocumentationType, OpenApi3.0 is maintaining multitype files uploading, and Swagger2 don't.

Next problem which I faced was:

415 Unsupported Media Type

Resolution:

First way: If you will use postman/javaScript framework -> you have to define media-type of every RequestPart (actually don't need for file, for file it will be set automatically). example in POSTMAN:

Second way: is implement maintaining of octet-stream, because spring set it by default in AbstractMessageConverterMethodArgumentResolver.class if there is no content-type header in request part. (I'm using spring-boot-starter-parent 2.4.1 -> spring-webmvc 5.3.2)

So here is 2 cases I found (to map response JSON to DTO):

1 case (not recommended): Set supporting to JSON converter:

2 case: Define new Converter class which will work definitely for needed classes (in my case it is MDVersionDTO.class) and definitely when it comes like octet-stream, to implement it, ovveride canRead() method.



来源:https://stackoverflow.com/questions/65666648/is-it-any-ability-to-upload-multipartfile-with-model-in-swagger-in-comment-415

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!