TransWikia.com

Spring Boot SwaggerUI with not working with latest version

Stack Overflow Asked on January 3, 2022

I am trying to add the Swagger UI in my Spring Boot Application, but I am unable to access the swagger-ui.html.

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
</parent>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-spring-webflux</artifactId>
        <version>3.0.0</version>
    </dependency>

Code:

@Configuration
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("com.learnings.search.web")).build().pathMapping("/")
                .enableUrlTemplating(false);
    }

}


@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, CassandraAutoConfiguration.class,
        KafkaAutoConfiguration.class })
@EnableSwagger2
public class SearchApp {

    public static void main(String[] args) {
        SpringApplication.run(SearchApp.class, args);
    }
}

I am able to access the swagger resources:

/swagger-resources
[
  {
    "name": "default",
    "url": "/v2/api-docs",
    "swaggerVersion": "2.0",
    "location": "/v2/api-docs"
  }
]


/swagger-resources/configuration/ui
{
  "deepLinking": true,
  "displayOperationId": false,
  "defaultModelsExpandDepth": 1,
  "defaultModelExpandDepth": 1,
  "defaultModelRendering": "example",
  "displayRequestDuration": false,
  "docExpansion": "none",
  "filter": false,
  "operationsSorter": "alpha",
  "showExtensions": false,
  "showCommonExtensions": false,
  "tagsSorter": "alpha",
  "validatorUrl": "",
  "supportedSubmitMethods": [
    "get",
    "put",
    "post",
    "delete",
    "options",
    "head",
    "patch",
    "trace"
  ],
  "swaggerBaseUiUrl": ""
}

Please let me know if something has changed in the latest version, because I have been using the swagger-ui in my other projects.

2 Answers

You need to add this dependency also If using swagger 3 version.

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
</dependency>

Also, add @EnableOpenApi annotation in main class in Springboot application.

Hope this will work. Thanks!

Answered by vikrant_2020 on January 3, 2022

In 3.0.0 swagger-ui.html has been moved to swagger-ui/index.html

Answered by jmhostalet on January 3, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP