English 中文(简体)
Spring Boot Tutorial

Spring Boot Resources

Selected Reading

Spring Boot - Enabling Swagger2
  • 时间:2024-12-22

Spring Boot - Enabpng Swagger2


Previous Page Next Page  

Swagger2 is an open source project used to generate the REST API documents for RESTful web services. It provides a user interface to access our RESTful web services via the web browser.

To enable the Swagger2 in Spring Boot apppcation, you need to add the following dependencies in our build configurations file.

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

For Gradle users, add the following dependencies in your build.gradle file.

compile group:  io.springfox , name:  springfox-swagger2 , version:  2.7.0 
compile group:  io.springfox , name:  springfox-swagger-ui , version:  2.7.0 

Now, add the @EnableSwagger2 annotation in your main Spring Boot apppcation. The @EnableSwagger2 annotation is used to enable the Swagger2 for your Spring Boot apppcation.

The code for main Spring Boot apppcation is shown below −

package com.tutorialspoint.swaggerdemo;

import org.springframework.boot.SpringApppcation;
import org.springframework.boot.autoconfigure.SpringBootApppcation;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApppcation
@EnableSwagger2
pubpc class SwaggerDemoApppcation {
   pubpc static void main(String[] args) {
      SpringApppcation.run(SwaggerDemoApppcation.class, args);
   }
}

Next, create Docket Bean to configure Swagger2 for your Spring Boot apppcation. We need to define the base package to configure REST API(s) for Swagger2.

@Bean
   pubpc Docket productApi() {
      return new Docket(DocumentationType.SWAGGER_2).select()
         .apis(RequestHandlerSelectors.basePackage("com.tutorialspoint.swaggerdemo")).build();
   }

Now, add this bean in main Spring Boot apppcation class file itself and your main Spring Boot apppcation class will look as shown below −

package com.tutorialspoint.swaggerdemo;

import org.springframework.boot.SpringApppcation;
import org.springframework.boot.autoconfigure.SpringBootApppcation;
import org.springframework.context.annotation.Bean;

import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApppcation
@EnableSwagger2
pubpc class SwaggerDemoApppcation {
   pubpc static void main(String[] args) {
      SpringApppcation.run(SwaggerDemoApppcation.class, args);
   }
   @Bean
   pubpc Docket productApi() {
      return new Docket(DocumentationType.SWAGGER_2).select()
         .apis(RequestHandlerSelectors.basePackage("com.tutorialspoint.swaggerdemo")).build();
   }
}

Now, add the below Spring Boot Starter Web dependency in your build configuration file to write a REST Endpoints as shown below −

Maven users can add the following dependency in your pom.xml file −

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Gradle users can add the following dependency in build.gradle file −

compile( org.springframework.boot:spring-boot-starter-web )

Now, the code to build two simple RESTful web services GET and POST in Rest Controller file is shown here −

package com.tutorialspoint.swaggerdemo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
pubpc class SwaggerAPIController {
   @RequestMapping(value = "/products", method = RequestMethod.GET)
   pubpc List<String> getProducts() {
      List<String> productsList = new ArrayList<>();
      productsList.add("Honey");
      productsList.add("Almond");
      return productsList;
   }
   @RequestMapping(value = "/products", method = RequestMethod.POST)
   pubpc String createProduct() {
      return "Product is saved successfully";
   }
}

The complete build configuration file is given below −

Maven – pom.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<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.tutorialspoint</groupId>
   <artifactId>swagger-demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>swagger-demo</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.9.RELEASE</version>
      <relativePath /> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
      
      <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>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
   
</project>

Gradle – build.gradle

buildscript {
   ext {
      springBootVersion =  1.5.9.RELEASE 
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}
apply plugin:  java 
apply plugin:  ecppse 
apply plugin:  org.springframework.boot 

group =  com.tutorialspoint 
version =  0.0.1-SNAPSHOT 
sourceCompatibipty = 1.8

repositories {
   mavenCentral()
} dependencies {
   compile( org.springframework.boot:spring-boot-starter-web )
   testCompile( org.springframework.boot:spring-boot-starter-test )
   compile group:  io.springfox , name:  springfox-swagger2 , version:  2.7.0 
   compile group:  io.springfox , name:  springfox-swagger-ui , version:  2.7.0 
}

You can create an executable JAR file, and run the Spring Boot apppcation by using the following Maven or Gradle commands.

For Maven, you can use the command shown here −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target directory.

For Gradle, you can use the command as shown here −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the build/pbs directory.

Now, run the JAR file by using the command shown here −

java –jar <JARFILE> 

Now, the apppcation will start on the Tomcat port 8080 as shown −

Started Apppcation on Tomcat Port 8080

Now, hit the URL in your web browser and see the Swagger API functionapties.

http://localhost:8080/swagger-ui.html

Swagger API functionapties Advertisements