Issues in Enabling the swagger 2 in Spring Boot java application

Viewed 1279

I hope you are all doing well. I am trying to enable the swagger in my spring boot and somehow I can not get it working. I took all the steps which I should have, it is really frustrating now to stick at it for a day. Here are the details.

  1. Added Dependencies
<dependency>
           <groupId>io.springfox</groupId>
           <artifactId>springfox-swagger-ui</artifactId>
           <version>2.9.2</version>
       </dependency>
       <dependency>
           <groupId>io.springfox</groupId>
           <artifactId>springfox-swagger2</artifactId>
           <version>2.9.2</version>
       </dependency>
  1. Enable the swagger annotation
@EnableSwagger2
  1. Create a bean with Docket Object to scan
@Bean
    public Docket swaggerApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("ml.XXXXXXXX"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(new ApiInfoBuilder().version("v1")
                        .title("Account Service API")
                        .description("Documentation Account Service API v1").build());
    }

My servlet context path is

server:
  servlet:
    context-path: /account

I have tried every possible URL of swagger like http://localhost:8080/swagger-ui.html, http://localhost:8080/account/swagger-ui.html, http://localhost:8080/account/v2/api-docs. None of this is working. Could you please let me know if I am missing anything or doing wrong. I would really appreciate it.

Thanks ~~

2 Answers

Swagger 2.9.2 is not working properly with spring boot version 2.0 and above, So I have switched to Open API.It requires only 2 steps

1.Add dependency

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.4.3</version>
</dependency>   
  1. Update/Set URL properties

     springdoc:
       version: '@springdoc.version@'
       api-docs:
         enabled: true
         path: /v3/api-docs
       swagger-ui:
         path: /swagger-ui.html
    

Can you try by updating dependencies to this

     <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>3.0.0-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>3.0.0-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-spring-webmvc</artifactId>
        <version>3.0.0-SNAPSHOT</version>
    </dependency>

And instead of @EnableSwagger2, replace it with @EnableSwagger2WebMvc

Related