24

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

Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
  • 1
    Have you tried http://localhost:8080/sdoc.jsp or http://localhost:8080/app/sdoc.jsp? – Ron Jan 09 '15 at 14:40
  • @Ron Just tried it now, but does not work. – Wim Deblauwe Jan 09 '15 at 15:15
  • Just followed what's written here - https://github.com/adrianbk/swagger-springmvc-demo/tree/master/swagger-ui - if you don't get a reply here, I'd suggest opening an issue on that repository. Adrian is very responsive. – Ron Jan 09 '15 at 15:17

8 Answers8

38

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.

Tamas
  • 446
  • 5
  • 5
4

I have swagger-ui v0.4 (with spring v4.14 & swagger-springmvc v0.9.4) working fine, although I had some similar problems at first. It seems like this class does the trick.

@Configuration
@EnableSwagger
public class SwaggerConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private SpringSwaggerConfig springSwaggerConfig;

    @Bean
    public SwaggerSpringMvcPlugin customImplementation() {
        return new SwaggerSpringMvcPlugin(springSwaggerConfig).apiInfo(
                apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfo(/* strings */);
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

I believe the relevant thing is the overridden configureDefaultServletHandling. And on my main WebApplicationInitializer, I have:

@Import(SwaggerConfig.class)

Finally, I fixed the issue with the UI's location box showing "http://localhost:8080${pageContext.request.contextPath}/api-docs" by including this in my dependencies:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <!--<version>8.0.15</version>-->
    <scope>provided</scope>
</dependency>

That provides something related to JSP processing. It is included in the dependencies of spring-boot, but it isn't normally provided.

Hope that helps.

Lucas Ross
  • 1,049
  • 8
  • 17
3

If you are experiencing this issue with version springfox swagger-ui 3.x or greater..

try the below url.. It worked for me..

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

For complete swagger documentations steps, refer: http://muralitechblog.com/swagger-rest-api-dcoumentation-for-spring-boot/

Muralidharan.rade
  • 2,226
  • 1
  • 24
  • 34
1

I have the same issue but this link works for me: http://localhost:8080/sdoc.jsp

It pre-populates the swagger ui resource url box with : http://localhost:8080${pageContext.request.contextPath}/api-docs

and when I hand edit it removing ${pageContext.request.contextPath} and hit Explore it shows me my api doc and I can even try my endpoints successfully. So definitely an issue but probably not picking up ${pageContext.request/contextPath}.

Looking at the source the javascript has: url: window.location.origin + "${pageContext.request.contextPath}/api-docs"

on a static swagger ui html I have this piece is coded as:

discoveryUrl:"./resource-list.json"

I hope this is a bit helpful

Borna
  • 11
  • 2
0

As indicated by Tamas above, the problem lies in using @EnableWebMvc, which goes around the default setup and skips some things Swagger needs. Switching that to @EnableSwagger2 for me was enough to fix the issues in my project, which showed similar symptoms.

DVA
  • 331
  • 2
  • 13
0

I have faced same issues in my project. resolved issue with below steps.

Added below dependencies in pom.xml

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

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

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

Added swagger2 ui configuration as below in the project level

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

Then able to get the swagger ui documentation http://localhost:8080/swagger-ui/

swagger api docs http://localhost:8080/v2/api-docs

Krishna V
  • 1,801
  • 1
  • 14
  • 16
-2

I have done separate config for Swagger and my problem was that without @EnableAutoConfiguration it was not working properly.

@Configuration
@EnableSwagger2
@EnableAutoConfiguration
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}
Wojtek
  • 1,210
  • 3
  • 18
  • 23
-3

I would suggest you to use @EnableSwagger2 tag and follow the steps and code from here: https://github.com/sanketsw/SpringBoot_REST_API

Also i am using following dependency which works perfectly fine:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.2.2</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.2.2</version>
    <scope>compile</scope>
</dependency>
Sacky San
  • 1,535
  • 21
  • 26
  • Linking to external resources is fine, but links can change dramatically or break entirely over time. Please include enough information to implement a solution in your answer without the need to rely on external information. – p.s.w.g Mar 10 '16 at 05:12
  • The detailed answer is here: http://stackoverflow.com/questions/34307496/advice-about-swagger-api/35907962#35907962 – Sacky San Jul 10 '16 at 09:19