5

Here's my pom.xml:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.7.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.7.0</version>
</dependency>

I am using version 1.5.3.RELEASE of Spring Boot. Here's my swagger config file:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket swagger() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

Here's my WebSecurityConfig.java:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security", "/swagger-ui.html", "/webjars/**");
}

When I do a get from the endpoint http://localhost:8080/v2/api-docs I get my JSON back:

{
  "swagger": "2.0",
  "info": {
    "description": "Api Documentation",
    "version": "1.0",
    "title": "Api Documentation",
    "termsOfService": "urn:tos",
    "contact": {},
    "license": {
      "name": "Apache 2.0",
      "url": "http://www.apache.org/licenses/LICENSE-2.0"
    }
  },
  "host": "localhost:8080",
  "basePath": "/",
  //ETC
}

But when I try to access the UI at localhost:8080/swagger-ui.html I get a blank page that looks like this: enter image description here

If I click on the page, I get promoted with this enter image description here

What am I doing wrong? Is this some sort of spring security issue?

Richard
  • 5,840
  • 36
  • 123
  • 208

4 Answers4

5

You can suggest API description path to Swagger in application config, using springfox.documentation.swagger.v2.path property, e.g. springfox.documentation.swagger.v2.path: /rest/docs in application.yml.

I've posted an example on github.

jihor
  • 2,478
  • 14
  • 28
  • this doesn't really help because I can already access api json from `http://localhost:8080/v2/api-docs`. I just can't get the swagger-ui.html to read from it – Richard Jun 17 '17 at 01:44
  • @Richard try changing `"/swagger-resources"` to `"/swagger-resources/**"` in your ignore list for web security, because Swagger goes to `/swagger-resources/configuration/ui` endpoint to get its config. – jihor Jun 17 '17 at 10:00
  • @Richard I've updated my github example with security config. – jihor Jun 17 '17 at 10:08
2

if You use Version - V3 || io.springfox >= 3.0.0

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

Java code

@Configuration
@EnableSwagger2

public class SwaggerConfig {

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2).select()
            .apis(RequestHandlerSelectors.basePackage("Your Controller package name"))
            .paths(PathSelectors.any()).build();
}

}

V3 browser URL -> http://localhost:8080/swagger-ui/#/ Run (Must need) : Mvn clean

Madhuka Dilhan
  • 1,396
  • 1
  • 14
  • 21
0

Its very likely spring-security is not allowing/preventing your endpoints to be discovered. Try changing your ant matchers to the following and see if that helps. The security/ui configuration endpoints were incorrect.

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers(
        "/v2/api-docs", 
        "/swagger-resources/configuration/ui",     
        "/swagger-resources", 
        "/swagger-resources/configuration/security", 
        "/swagger-ui.html",     
        "/webjars/**");
}
Dilip Krishnan
  • 5,417
  • 3
  • 37
  • 53
  • 1
    I think it's a spring security issue too but this unfortunately didn't work – Richard Jun 17 '17 at 01:33
  • Can you help me on this ? https://stackoverflow.com/questions/55516559/swagger-api-tag-description-not-coming – Kick Apr 04 '19 at 14:05
-1

change the swagger version to 2.9.2 it will work.

   <dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger2</artifactId>
     <version>2.9.2</version>
   </dependency>