Richard
Richard

Reputation: 6126

Spring Boot Swagger API not working

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?

Upvotes: 5

Views: 15120

Answers (4)

Madhuka Dilhan
Madhuka Dilhan

Reputation: 1416

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

Upvotes: 2

Aqil Aghamirzayev
Aqil Aghamirzayev

Reputation: 119

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>

Upvotes: -1

Dilip Krishnan
Dilip Krishnan

Reputation: 5487

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/**");
}

Upvotes: 0

jihor
jihor

Reputation: 2599

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.

Upvotes: 5

Related Questions