是否可以在 Swagger 中上传带模型的 MultipartFile?(在评论 415 Unsupported Media Type Resolved) [英] Is it any ability to upload MultipartFile with Model in Swagger? (In comment 415 Unsupported Media Type Resolved)
问题描述
我正在使用最后一个(也是唯一一个)
I'm using the last (and only)
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
带弹簧靴
<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:
在这种情况下,在 swagger-ui 上,我只看到没有模型的文件上传:
in this case on swagger-ui I see only file uploading without model:
我在邮递员中试过这个案例,它工作正常
I tried this case in postman and it works fine
这是一个错误吗?或者我应该添加一些其他注释以使其正常工作?
Is it a bug? Or should I add some other annotations to make it work?
推荐答案
问题出在配置类中,在 Docket Bean 中.在这里,我定义了到我的控制器的路由,以从 swagger-ui 中排除其他人.问题出在DocumentationType,OpenApi3.0在维护多类型文件上传,而Swagger2没有.
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 不支持的媒体类型
分辨率:
第一种方式:如果您将使用 postman/javaScript 框架 ->您必须定义每个 RequestPart 的媒体类型(实际上不需要文件,文件会自动设置).邮递员中的示例:
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:
第二种方式:是实现八位字节流的维护,因为如果请求部分没有content-type header,spring默认在AbstractMessageConverterMethodArgumentResolver.class
中设置它.(我正在使用 spring-boot-starter-parent 2.4.1 -> spring-webmvc 5.3.2
)
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
)
所以这是我发现的 2 个案例(将响应 JSON 映射到 DTO):
1 个案例(不推荐):设置对 JSON 转换器的支持:
1 case (not recommended): Set supporting to JSON converter:
2 例:定义新的 Converter 类,它肯定适用于所需的类(在我的情况下是 MDVersionDTO.class
),并且肯定会像 octet-stream,为了实现它,ovveride canRead()
方法.
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.
这篇关于是否可以在 Swagger 中上传带模型的 MultipartFile?(在评论 415 Unsupported Media Type Resolved)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!