springfox: api_docs shows content but swagger-ui (2.6.1) is empty

Hi this is a question on configuration. I was reading for hours today but did not get my mistake.

Symptom:

http://localhost:8080/sabi/api/v2/api-docs/ shows me that the annotations have been scanned and assembled. I get a detailed json containing my content.

However when calling the swagger-ui http://localhost:8080/sabi/swagger-ui.html The ui renders, but I can see non of my API. Even by pasting the api-docs url from above and pressing enter did not lead to any result.

Here’s my setup:

Im using:

           <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.6.1</version>
                <scope>compile</scope>
            </dependency>

            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.6.1</version>
                <scope>compile</scope>
            </dependency>

with spring-webmvc (4.2.1-RELEASE) and using xml-free annotation based config:

@Configuration
@EnableWebMvc
@EnableSwagger2
@ComponentScan(basePackages = "de.bluewhale.sabi")
@PropertySource("classpath:server.properties")
public class AppConfig {

    @Autowired
    Environment env;

    @Bean
    public EncryptionService encryptionService() {
        return new EncryptionService(env.getProperty("accessToken.salt"), env.getProperty("accessToken.password"));
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

and


@Configuration
public class ApiDocumentationConfiguration {

    @Bean
    public Docket documentation() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    // .paths(regex("/api/*"))
                    .build()
                .pathMapping("/api")
                .apiInfo(metadata());
    }

    private ApiInfo metadata() {
        return new ApiInfoBuilder()
                .title("sabi's REST API documentation")
                .description("see https://github.com/StefanSchubert/sabi")
                .version("1.0")
                .license("MIT Licence (MIT)")
                .licenseUrl("https://github.com/StefanSchubert/sabi/blob/master/LICENSE")
                .contact("Stefan.Schubert@bluewhale.de")
                .build();
    }
}

What am I doing wrong?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 42 (15 by maintainers)

Commits related to this issue

Most upvoted comments

Finally it’s working for me too. I needed to throw “api” as pathMapping of the docket config:

    @Bean
    public Docket documentation() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    // .paths(regex("/api/*"))
                    .build()
                .pathMapping("/")
                .apiInfo(metadata());
    }

Also I have api as part of the base path of my controllers:

@RestController
@RequestMapping(value = "api/user")
public class AuthenticationController {
...
}

But the most important thing to do was this in my app-config:

@Configuration
// @EnableWebMvc  IF ENABLED SWAGGER-UI WON'T WORK
@EnableSwagger2
@ComponentScan(basePackages = "de.bluewhale.sabi")
@PropertySource("classpath:application.properties")
public class AppConfig { 

Our Spring (but not spring-boot) app uses the @ annotation EnableWebMvc. To make swagger work we add a config class:

@Configuration
@EnableWebMvc
public class SwaggerMVCConfig extends WebMvcConfigurerAdapter {

    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}

Content Security Policy - Just in case this helps, I had this. Check your CSP, I disabled ‘unsafe-eval’ or similar.

TL;DR Clear browser cache

I had the same situation (spring-boot 1.5.8.RELEASE, springfox-swagger-ui 2.7.0). Opened the same page http://localhost:8080/swagger-ui.html in firefox - and it worked. Console in chrome shows that there is an error:

“Template was precompiled with an older version of Handlebars than the current runtime. Please update your precompiler to a newer version (>= 4.0.0) or downgrade your runtime to an older version (>= 2.0.0-beta.1).”

Error at Object.<anonymous> (http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:527:24) at __webpack_require__ (http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:56:30) at Object.exports.__esModule (http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:266:19) at __webpack_require__ (http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:56:30) at Object.exports.default.newObj (http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:173:24) at __webpack_require__ (http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:56:30) at Object.exports.default.obj.__esModule.default (http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:89:27) at __webpack_require__ (http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:56:30) at http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:76:18 at http://localhost:8080/webjars/springfox-swagger-ui/lib/handlebars-4.0.5.js:79:10

In case this help anyone - I encountered this problem after refactoring a Spring Boot project to deploy a WAR to a standalone Tomcat (rather than use the default embedded servlet container). The issue was my @SpringBootApplication class wasn’t extending SpringBootServletInitializer

In that case try accessing the swagger-ui here http://localhost:8080/sabi/api/swagger-ui.html

When hitting enter I can see an HTTP-404 on the developer console of firefox: http://localhost:8080/sabi/swagger-resources/configuration/ui

with this information I found: https://github.com/springfox/springfox/issues/983

and tried to access: http://localhost:8080/sabi/api/swagger-resources/configuration/ui which results in some JSON and a HTTP-200.