1. 程式人生 > 其它 >使用swagger或者knife4j在2.0.X或3.0.X版本上傳檔案域不出現的問題,請求格式自動預設為body的問題

使用swagger或者knife4j在2.0.X或3.0.X版本上傳檔案域不出現的問題,請求格式自動預設為body的問題

解決方法

其中最關鍵的是: 引數 paramType="query"

@ApiImplicitParam(name = "file", value = "檔案流物件,接收陣列格式", required = true, dataType = "__File",paramType = "query")
    @PostMapping("/importData")
    @ResponseBody
    public Result uploadFile(@RequestParam(value = "file") MultipartFile[] files) throws Exception{
        MultipartFile file 
= files[0]; }

原因:

暫時推測為:openApi3.0和Swagger不相容導致

 

解決之後的效果:

 

knfie4j的配置檔案如下:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2
public class Knife4jConfiguration { @Bean(value = "defaultApi2") public Docket defaultApi2() { Docket docket=new Docket(DocumentationType.SWAGGER_2).enable(true) .apiInfo(new ApiInfoBuilder() .title("XX匯入服務") .description("XX匯入服務") .termsOfServiceUrl("") .contact(new Contact("lzy","","[email protected]")) .version("1.0") .build()) //分組名稱 .groupName("1.0版本") .select() //這裡指定Controller掃描包路徑 .apis(RequestHandlerSelectors.basePackage("core.controller")) .paths(PathSelectors.any()) .build(); return docket; } }

其中引用了springfox包,其中引用了openApi的相關實現程式碼