How to make swagger-ui.html your app web-service’s index page

stanley obidiagha
2 min readSep 28, 2018

--

I was introduced to swagger api which is a very good tool for web service documentation. Considering that i build java application web-service using spring boot framework, it was very easy to integrate and it rendered my apis as expected but i had to view the swagger api doc page by calling the swagger-ui.html page as part of the url which was not ok for me because i was considering the web service consumers. How it wont be easy for them to remember that the url should include swagger-ui.html for them to view the web-service doc.

So, i decided to do something which is to ensure that when the consumers of the web-service call the context path of the application, they are automatically navigated to the swagger-ui.html page, this made me happy then i decided to share.

Please note that this is implemented in java using spring boot framework.

Please view below details

We need to add the dependency as usual.

<!-- swagger -->
<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>

Then i created a class for swagger configuration which i called the swaggerConfig.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.build();
}

private ApiInfo getApiInfo(){
return new ApiInfoBuilder()
.title("Simple app")
.description("Simple app documentation page, This is for test purpose. like a testing field")
.version("0.0.1")
.contact(new Contact("Stob", "http://www.stob.com", "info@stob.com"))
.build();
}

}

With these done, we can view our swagger-ui.html page BUT we have to add to the url “/swagger-ui.html”, for example http://localhost:8080/swagger-ui.html instead of http://localhost:8080/ or http://localhost:8080 (our target)

To automatically navigate to the swagger-ui.html page, we use a request filter.

import org.apache.catalina.servlet4preview.http.HttpServletRequest;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyFilter implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
if (request.getRequestURI().equals("") || request.getRequestURI().equals("/")){
response.sendRedirect(Constants.DOCUMENTATION_URL);
return;
}


filterChain.doFilter(servletRequest, servletResponse);
}

@Override
public void destroy() {

}
}
public interface Constants {

String DOCUMENTATION_URL = "/swagger-ui.html";

}
@SpringBootApplication
public class SimipleWebappApplication {

public static void main(String[] args) {
SpringApplication.run(SimipleWebappApplication.class, args);
}

@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
MyFilter myFilter = new MyFilter();
filterRegistrationBean.setFilter(myFilter);
return filterRegistrationBean;
}

}

Visit your api or application web-service swagger doc using the context path “http://localhost:8080/” or without the context path “http://localhost:8080”.

I hope this helps someone. Thank you.

--

--

Responses (1)