'Springfox swagger-ui 3.0.0 does not bring up swagger-ui.html page
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>
Solution 1:[1]
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.
Solution 2:[2]
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'
}
Solution 3:[3]
Figured the solution.
Changing the method as follows, starts showing the 'Choose File' button
public ResponseEntity<ByteArrayResource> method(
@ApiParam(name = "file", value = "Select the file to Upload", required = true)
@RequestParam("file") MultipartFile file, @RequestParam("id") String id) {
...
}
Solution 4:[4]
By changing @RequestParam to @RequestPart can solve the problem.
for a single file -> @RequestPart("file") MultipartFile file
for multiple files -> @RequestPart("file") MultipartFile[] files
Solution 5:[5]
I had the same problem and wanted to share my solution. After I added the dependency
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
to my pom.xml I had to execute a mvn clean install
.
Now it works.
Solution 6:[6]
With Spring Boot 2.2.6, I used only these 2 swagger dependencies in my pom.xml and no swagger annotations.
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
I also added this class to my applcation:
@Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
// URL is http://localhost:8080/swagger-ui/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Juan Rojas |
Solution 3 | SSK |
Solution 4 | Buddhika Chandrasiri |
Solution 5 | Panderas |
Solution 6 | splashout |