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 &