Added Springfox Swagger-UI and it's not working, what am I missing?

51,305

Solution 1

I ran into this issue because I had endpoints with request mappings that had path variables of this form: /{var}. Turns out that this is an issue for both GET and POST endpoints i.e. GET /{var} and POST /{var} block swagger-ui. Once I made the paths more specific, I got swagger-ui to work.

Quote from https://github.com/springfox/springfox/issues/1672

When spring finds a simple path with just one variable swagger cannot intercept the URLs.

Found by investigating various ideas in comments.

Solution 2

Springfox 3.0.0 only works with Spring Boot <= 2.6.0-M2 but not with versions above it

Options for Spring Boot 2.6 M2 <
1. spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER #-> App.properties
    -    But without Actuator
2. @EnableWebMvc
3. Migrate to springdoc-openapi-ui - same steps as io.springfox >= 3.X

io.springfox >= 2.X

io.springfox >= 3.X

<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>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-schema</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
browser URL
http://localhost:8080/swagger-ui.html
browser URL
http://localhost:8080/swagger-ui/
http://localhost:8080/swagger-ui/index.html
Must Need

mvn clean

Must Need

mvn clean

@Configuration
@EnableSwagger2
@Configuration
@EnableSwagger2

Solution 3

I tried most of these answers and the final solution was creeping..

The right URL is the following

http://localhost:8080/swagger-ui/

I'm using Springfox swagger-ui 3.x.x

Refer for complete swagger setup: http://muralitechblog.com/swagger-rest-api-dcoumentation-for-spring-boot/

Solution 4

Already a lot of answers have stated the right answer but still, there has been some confusion regarding the error.

If you are using Spring Boot Version >= 2.2, it is recommended to use SpringFox Swagger version 3.0.0

Now, only a single dependency is required to be added in the pom.xml.

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

Once the application is started, you can get the documentation by hitting either of the new swagger URLs.

Option 1: http://localhost:8080/swagger-ui/

Option 2: http://localhost:8080/swagger-ui/index.html

Solution 5

For Spring Version >= 2.2, you should add the dependency springfox-boot-starter

pom.xml:

<properties>
    <java.version>1.8</java.version>
    <io.springfox.version>3.0.0</io.springfox.version>
</properties>

<dependencies>
    <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>${io.springfox.version}</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-bean-validators</artifactId>
        <version>${io.springfox.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>${io.springfox.version}</version>
    </dependency>
</dependencies>

ApplicationSwaggerConfig

@Configuration
@EnableSwagger2
public class ApplicationSwaggerConfig {

    @Bean
    public Docket employeeApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

}

Swagger-UI link: http://localhost:8080/swagger-ui/index.html#/

Share:
51,305
pupeno
Author by

pupeno

You can find my blog at https://pupeno.com where I publish about coding and other stuff.

Updated on February 16, 2022

Comments

  • pupeno
    pupeno about 2 years

    Following the instructions here:

    http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

    I added these dependencies to my project:

    compile "io.springfox:springfox-swagger2:2.7.0"
    compile "io.springfox:springfox-swagger-ui:2.7.0"
    

    and configured SpringFox Swagger like this:

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    .build();
        }
    }
    

    but the Swagger UI seems not to get enabled. I tried:

    and all I get is:

    Whitelabel Error Page
    
    This application has no explicit mapping for /error, so you are seeing this as a fallback.
    
    Mon Sep 11 09:43:46 BST 2017
    There was an unexpected error (type=Method Not Allowed, status=405).
    Request method 'GET' not supported
    

    and on the logs I see:

    2017-09-11 09:54:31.020  WARN 15688 --- [nio-8080-exec-6] o.s.web.servlet.PageNotFound             : Request method 'GET' not supported
    2017-09-11 09:54:31.020  WARN 15688 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
    

    http://localhost:8080/swagger-resources returns:

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

    What am I missing?

  • Edouard Menayde
    Edouard Menayde over 3 years
    The / at the end is the very important part
  • sebnukem
    sebnukem over 3 years
    OMFG this is unbelievable. My entire weekend wasted over this. Thank you for this solution.
  • Abhishek
    Abhishek over 3 years
    Adding / at the end of url for swagger-ui worked for me.
  • Arvind Kumar
    Arvind Kumar over 3 years
    Thank you very much. This saved my day. I was entering URL as "localhost:8080/swagger-ui" and couldn't get the UI but with "/" at the end, it rendered like magic. Could you plz explain this "/" at the end? Is it mandatory?
  • viveknaskar
    viveknaskar about 3 years
    Glad that you found this useful. Yes, with the new release, the "/" at the end is mandatory. You can refer to this blog as well: baeldung.com/swagger-2-documentation-for-spring-rest-api
  • Datz
    Datz almost 3 years
    worked for me with url localhost:8181/<base-path>/swagger-ui/index.html
  • stoneshishang
    stoneshishang over 2 years
    This didn't work for me at the first place. but it worked after few try. I have no clue why. I'm facing the same issue after following the tutorial on youtube.com/…. one of the comments used this method to resolve the same issue.
  • HuserB1989
    HuserB1989 over 2 years
    It isn't working for me. Still getting 401. Basically, when I try to enter localhost:8080 it asks for username and password which is very weird. I can only use postman to check things but not url.
  • Muralidharan.rade
    Muralidharan.rade over 2 years
    @HuserB1989 maybe Spring Security is enabled. you can remove the dependency and try
  • Doctor Who
    Doctor Who over 2 years
    I have done all these steps still swagger is showing 404
  • SteveG
    SteveG about 2 years
    Thats fixed it for me, I also reomoved the old swagger dependencies. Thanks ! implementation("io.springfox:springfox-swagger2:${swaggerVer‌​sion}") implementation("io.springfox:springfox-swagger-ui:${swaggerV‌​ersion}")
  • muhrahim95
    muhrahim95 almost 2 years
    Thank you! This helped me fix it after a lot of time spent