2
votes

I am working on setting up swagger2 documentation for my Spring REST Project. But when I try to execute http://localhost:8085/swagger-ui.html returns empty page.The problem is my bean class is not loaded by my spring MVC application and it is NOT a spring boot application.

The below is my swagger2 config class

@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig extends WebMvcConfigurerAdapter{

    @Bean
    public Docket newsApi() {
        System.out.println("!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@22");
    return new Docket(DocumentationType.SWAGGER_2)
    .groupName("Dialr")
    .apiInfo(apiInfo())
    .select()
    .paths(regex("/*.*"))
    .build();
    }

    private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
    .title("Spring REST Sample with Swagger")
    .description("Spring REST Sample with Swagger")
    .termsOfServiceUrl("http://www-03.ibm.com/software/sla/sladb.nsf/sla/bm?Open")
    .contact("Niklas Heidloff")
    .license("Apache License Version 2.0")
    .licenseUrl("https://github.com/IBM-Bluemix/news-aggregator/blob/master/LICENSE")
    .version("2.0")
    .build();
    }

}

The below is my rest-servlet.xml under WEB-INF

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
 <context:component-scan base-package="com.xx.yy.zz" />

 <mvc:annotation-driven />
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

<context:annotation-config/>
 <bean name="swaggerConfig" class="com.xx.yy.SwaggerConfig"/>

  </beans>

Please find the web.xml entry as well

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Retail_SVC</display-name>


  <servlet>
 <servlet-name>rest</servlet-name>
    <servlet-class>
    org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/rest-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>rest</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

Please let me know if anything is missing ? Any help is highly appreciated.

2
Couple of comments: you don't need to extends WebMvcConfigurerAdapter, since you're not doing anything with it. If you're component scanning then you don't need to explicitly declare the swaggerConfig bean. Lastly since you're overriding the dispatch servlet you might try hitting http://localhost:8085/rest/swagger-ui.html - Dilip Krishnan

2 Answers

0
votes

Since the base url mapping in your servlet mapping is /rest/* the swagger-ui.html will not load to http://localhost:8085/swagger-ui.html so try with http://localhost:8085/rest/swagger-ui.html it may work

0
votes

I used the WebMvcConfigurer instead of the WebMvcConfigurerAdapter because that class is already deprecated.

@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
  @Bean
  public Docket productApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()                
            .apis(RequestHandlerSelectors.basePackage("com.illary.controller"))
            .paths(PathSelectors.any())
            .build()            
            .apiInfo(metaData());
  }
  private ApiInfo metaData() {
    return new ApiInfoBuilder()
            .title("Spring Boot Swagger App")
            .description("\"Spring Boot Swagger Server App\"")
            .version("1.0.0")
            .license("Apache License Version 2.0")
            .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0\"")
            .build();
  }

  public ApiInfo apiInfo() {
    final ApiInfoBuilder builder = new ApiInfoBuilder();
    builder.title("Swagger Test App").version("1.0").license("(C) Copyright Test")
    .description("The API provides a platform to query build test swagger api");

    return builder.build();
  }

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