Swagger Tutorials & Notes
Table of Contents
What is the URL to open Swagger UI ?
https://localhost:8080/ngdeveloper/api/swagger-ui.html
What is the URL to view the list of API’s as JSON in Swagger ?
https://localhost:8080/ngdeveloper/api/v2/api-docs
How to disable Swagger related URL’s in Spring Security ?
In your WebSecurityConfig class make sure to skip the below endpoints/urls,
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/actuator/**", "/v2/api-docs", "/webjars/**", "/swagger-resources/**","/swagger-ui.html");
}
In very few case it still throws the exceptions then try “**/swagger-resources/**” instead of “/swagger-resources/**”
How to disable Swagger UI in Production ?
Swagger UI can be enabled or disabled based on the profile. Make sure you have separate swagger configuration class and annotated with
@Profile("dev")
Now only for the profile dev it displays the Swagger UI.
Those who don’t know about the swagger config class below class is shared,
package com.ngdeveloper.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import com.google.common.base.Predicates;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@Configuration
@Profile("dev")
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot"))).apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build().apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("NgDeveloper's API Docs").description("Explore NgDeveloper's API's and its uses").version("1.0").termsOfServiceUrl("http://ngdeveloper.com/").build();
}
}