Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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>
like image 822
user1805280 Avatar asked Aug 06 '20 00:08

user1805280


2 Answers

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

like image 192
Thang Le Avatar answered Oct 14 '22 20:10

Thang Le


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'
}
like image 21
Juan Rojas Avatar answered Oct 14 '22 18:10

Juan Rojas