I am using springfox swagger-ui with Springboot but the fileUpload button is not enabled for multipart upload. I tried upgrading to springfox-swagger-ui 3.0.0 but that does not even bring up the swagger-ui page. Is there any way to get the file upload button ?
My API call looks like this :
@RequestMapping(value = "/foo", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<ByteArrayResource> method(@RequestParam("file") MultipartFile file, @RequestParam("id") String id) {
.... }
Current issue with springfox-swagger-ui 2.10.5
My pom.xml is :
<properties>
<java.version>1.8</java.version>
<io.springfox.version>3.0.0</io.springfox.version>
</properties>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${io.springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.10.5</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-data-rest</artifactId>
<version>${io.springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>${io.springfox.version}</version>
</dependency>
From: http://springfox.github.io/springfox/docs/current/#migrating-from-existing-2-x-version
there are things need to do:
1. add springfox-boot-starter to POM , remove old dependencies from POM: springfox-swagger2 and springfox-swagger-ui
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
2. Remove the @EnableSwagger2 annotations
@Configuration
// remove @EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage(UserManagementImpl.class.getPackage().getName()))
.paths(PathSelectors.any()).build();
}
}
3. using @RequestPart("files") on request
@ApiOperation(value = "files", notes = "upload user emails from CSV and email content from json and send out")
@PostMapping(path = "/users/uploadUserEmailsAndSend", consumes = { "multipart/form-data" })
@ResponseBody
ResponseEntity<UploadUsersResDTO> uploadUserEmailsAndSend(@RequestPart("files") MultipartFile[] filesUpload){
CSVParser csvParser = null;
BufferedReader fileEmailsReader = null;
BufferedReader fileEmailMessageReader = null;
MultipartFile fileCSV = null;
MultipartFile fileJson = null;
try {
if (filesUpload[0].getOriginalFilename().toLowerCase().endsWith("csv")) {
fileCSV = filesUpload[0];
fileJson = filesUpload[1];
} else {
fileCSV = filesUpload[1];
fileJson = filesUpload[0];
}
// more codes ....
}
4. access swagger at: http://localhost:8080/swagger-ui/
5. Last: you should move to OpenAPI (https://springdoc.org/)
OpenAPI is top on swagger, so if you are using Spring boot, the OpenAPI configuration is much simpler than Springfox.
Quoted from http://springfox.github.io/springfox/docs/current/#changes-in-swagger-ui
swagger-ui location has moved from http://host/context-path/swagger-ui.html to http://host/context-path/swagger-ui/index.html OR http://host/context-path/swagger-ui/ for short. This makes it work much better with pulling it as a web jar and turning it off using configuration properties if not needed.
for maven
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>
for gradle
dependencies {
compile 'io.springfox:springfox-swagger-ui:3.0.0'
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With