I have a Spring Boot REST API application and I integrated Swagger for documentation and also using it to test API with Swagger-UI.
Now my task was to disable the Swagger-UI on our production environment (public domain) and enable it in our dev environment which was on private IP.
APPROACH – 1
With Swagger vr-3.0.0 we can add springfox.documentation.enabled=false/true in corresponding environment profile application.properties file.
Like for prod server application-prod.properties file
springfox.documentation.enabled=false
And for dev server application-dev.properties file
springfox.documentation.enabled=true
And run the app, by specifing the profile in VM arguments
-Dspring.profiles.active=prod/dev
APPROACH – 2
To Achieve it, I did the below mentioned changes in my Swagger Config class and in application.properties file.
My pom.xml, where I added the below dependencies to integrate swagger.
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
In my application-dev.properties file, I added one key-value pair with boolean value-true
use-swagger=true
and in my application-prod.properties file, I changed the value to false
use-swagger=false
Now, finally in my SwaggerConfig class, I used the above key to enable/disable my Swagger-UI
@Configuration
public class SwaggerConfig implements EnvironmentAware {
private Environment environment;
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.jkoder.example.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo())
.enable(Boolean.parseBoolean(environment.getProperty("use-swagger")));
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
}
enable(Boolean.parseBoolean(environment.getProperty(“use-swagger”))) this function lets you enable or disable the swagger-ui in the required environment.
To execute the Spring Boot REST API application, we were using the below commands
In Dev environment-
nohup java -jar -Dspring.profiles.active=dev target/myapp-1.0.jar &
In Production environment-
nohup java -jar -Dspring.profiles.active=prod target/myapp-1.0.jar &
APPROACH – 3
Just annotate your configuration with @Profile annotation
@Profile({"local", "dev"})
@Profile({"local", "dev"})
@Configuration
public class SwaggerConfig implements EnvironmentAware {
private Environment environment;
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.jkoder.example.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo())
.enable(Boolean.parseBoolean(environment.getProperty("use-swagger")));
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
}
And run the app, by specifing the profile in VM arguments
-Dspring.profiles.active=prod/dev
More Tutorials on Swagger –
How to document Controller and Model class using Swagger2 in Spring Boot REST project
Integrate Swagger2 with Spring Boot REST API
Hide an End-points from Swagger Documentation in Spring Boot REST API
Jkoder.com Tutorials, Tips and interview questions for Java, J2EE, Android, Spring, Hibernate, Javascript and other languages for software developers