Unable to get Swagger UI working with Spring boot

Wim Deblauwe picture Wim Deblauwe · Jan 9, 2015 · Viewed 40.9k times · Source

I am trying to get Swagger UI working with Spring Boot 1.2.1. I followed the instructions at https://github.com/martypitt/swagger-springmvc and I added @EnableSwagger on my spring config.

I currently get back JSON when I go to http://localhost:8080/api-docs but no nice HTML.

I am using Maven and added the dependency on swagger-ui:

<dependency>
    <groupId>org.ajar</groupId>
    <artifactId>swagger-spring-mvc-ui</artifactId>
    <version>0.4</version>
</dependency>

This is my complete list of dependencies:

<dependencies>
        <dependency>
            <groupId>com.mangofactory</groupId>
            <artifactId>swagger-springmvc</artifactId>
            <version>0.9.4</version>
        </dependency>
        <dependency>
            <groupId>org.ajar</groupId>
            <artifactId>swagger-spring-mvc-ui</artifactId>
            <version>0.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

I also tried http://localhost:8080/docs/index.html as URL, but that just gives the "Whitelabel Error Page"

Update:

I created a test project on Github to show the problem: https://github.com/wimdeblauwe/springboot-swagger-test

Answer

Tamas picture Tamas · Mar 12, 2015

Your problem lies in your SwaggerConfiguration file. You need to take out @EnableWebMvc, because this causes the default Spring Boot view resolver to be overwritten by the default 'SpringWebMvc' one which serves static content differently.

By default, Spring Boot will serve static content from any of the following directories:

  • /META-INF/resources/
  • /resources/
  • /static/
  • /public/

including webjars.

I had the same problem and I found this in the documentation: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-auto-configuration

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc. If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @Bean of type WebMvcConfigurerAdapter, but without @EnableWebMvc.

I hope this helps.