Spring Boot Swagger Profile (2024)
In previous tutorial, we have developed Spring Boot + Swagger 2 Example.
Spring Profiles enables users to register beans based on their profile (dev, test, prod etc). As such, when the application is in DEVELOPMENT, only some beans can be loaded, whereas when it is in PRODUCTION, other beans can be loaded. Assume we want the Swagger documentation to be enabled only in the QA environment and disabled in all others. Profiles can be used to accomplish this. Spring Boot makes it very simple to use Profiles.
In this tutorial, will demonstrate how to enable or disable the Swagger documentation for specific environments.
Spring Boot Swagger Tutorial :
- Spring Boot + Swagger 2 Example
- Spring Boot + Swagger + Profile Example
- Spring Boot Swagger 3 Example
- Spring Boot Swagger 3 + Security Example
- Swagger Interview Questions and Answers
What is Swagger?
Swagger is a JSON-based interface description language for specifying RESTful APIs for microservices/spring boot applications. To design, construct, describe, and consume RESTful web services, Swagger is used in conjunction with a set of open-source software tools. Automated documentation, code generation, and test-case generation are all included in Swagger.
Let's create Spring Boot Project from Spring Initializer site https://start.spring.io/
Project Structure
Maven Dependency
Add springfox-swagger2
and springfox-swagger-ui
dependencies for swagger2
in pom.xml file.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.techgeeknext</groupId>
<artifactId>spring-boot-swagger2-profile</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Take a look at our suggested posts:
Application Properties
Declare the active profile as qa
in the application.properties
file, so
that swagger documentation will be enable for only QA environment and disable for all other
environments like production, development
etc.
spring.profiles.active=qa
Spring Boot disable swagger in Production
To disable the swagger in Production environment, we can specify in SwaggerConfig
using
@Profile("!prod")
.
Swagger2 Configuration
Add Class to manage swagger configuration. We can enable the swagger documentation for the specific
environment by using @Profile
annotation
package com.techgeeknext.config;
import com.google.common.base.Predicate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;
@Profile("qa")
//@Profile({"dev","qa"}) //make it enable for multiple environments
//@Profile("!prod") // enable for all environment except Production environment
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket postsApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("emp-api")
.apiInfo(swaggerDocumentationDetails())
.select()
.paths(restApis())
.build();
}
private Predicate<String> restApis() {
return or(regex("/api/anonymous-user/.*")
, regex("/api/home-page.*"));
}
private ApiInfo swaggerDocumentationDetails() {
return new ApiInfoBuilder()
.title("TechGeekNext Swagger API")
.description("TechGeekNext - Swagger Profile Example")
.version("1.0")
.contact("TechGeekNext")
.license("TechGeekNext Swagger Example License")
.licenseUrl("contact@techgeeknext.com")
.version("1.0")
.build();
}
}
Rest Controller
Create controller class to test swagger documentation using rest endpoint.
package com.techgeeknext.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Swagger2RestController {
@RequestMapping(method = RequestMethod.GET, value = "/api/home-page")
public String homePage() {
return "TechGeekNext - Welcome to Swagger Profile Example!";
}
}
Swagger2 UI Docs
- Test without QA active profile : Remove or comment the active profile from
application.properties
file and run the Spring Boot application.
After testing the http://localhost:8080/swagger-ui.html, it'll not show the swagger documentation for the project as shown below:## comment active profile for testing #spring.profiles.active=qa
- Test with QA Profile : Now add the active profile as
qa
inapplication.properties
.
And run the spring boot application, and we can see that swagger documentation will be shown.spring.profiles.active=qa
Download Source Code
The full source code for this article can be found on below.Download it here - Spring Boot Swagger Profile Example