12

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 Upload file button missing

enter image description here

    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>
user1805280
  • 251
  • 1
  • 5
  • 14

6 Answers6

21

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/

enter image description here

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.

enter image description here

Thang Le
  • 1,419
  • 1
  • 17
  • 25
  • 3
    Plus 1 after surfing for 3 hours on google come to the right place. Thank you – Qaseem Saleem Feb 08 '21 at 11:10
  • Thanks, rpelacing RequestParam with RequestPart did the job. – ivop Apr 07 '21 at 11:03
  • If // remove `@EnableSwagger2` is not done: all still seems to work; what does removing `@EnableSwagger2` do? – Treefish Zhang Aug 09 '21 at 21:14
  • It is redundant after updating to swagger 3.0. I guess you can keep no harm. No use though. – Manonandan S K Feb 23 '22 at 11:40
  • Note: URL `http://localhost:8080/swagger-ui/` doesn't work if you have `@EnableWebMvc` and you have to use `http://localhost:8080/swagger-ui/index.html` explictly (see https://stackoverflow.com/a/55629558/685806 and also https://github.com/springdoc/springdoc-openapi/issues/236) – Pino Mar 16 '22 at 15:26
  • The last point seems very important since actually Springfox 3.0.0 doesn't work with spring-boot 2.6.x (see https://github.com/springfox/springfox/issues/3934) and someone says it is abandoned (see https://stackoverflow.com/a/66640426/685806). The migration guide is at https://springdoc.org/#migrating-from-springfox – Pino Mar 16 '22 at 17:05
3

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'
}
Juan Rojas
  • 8,711
  • 1
  • 22
  • 30
0

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) {
   
   ...
}
SSK
  • 3,444
  • 6
  • 32
  • 59
user1805280
  • 251
  • 1
  • 5
  • 14
0

By changing @RequestParam to @RequestPart can solve the problem.

  • for a single file -> @RequestPart("file") MultipartFile file

  • for multiple files -> @RequestPart("file") MultipartFile[] files

0

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.

Panderas
  • 55
  • 1
  • 8
0

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/
splashout
  • 537
  • 5
  • 11