Spring Boot 启用Swagger2

  • 启用Swagger2

    Swagger2是一个开源项目,用于生成RESTful Web服务的REST API文档。它提供了一个用户界面,用于通过Web浏览器访问我们的RESTful Web服务。
    要在Spring Boot应用程序中启用Swagger2,您需要在我们的构建配置文件中添加以下依赖项。
    
    <dependency>
       <groupId>io.springfox</groupId>
       <artifactId>springfox-swagger2</artifactId>
       <version>2.9.2</version>
    </dependency>
    <dependency>
       <groupId>io.springfox</groupId>
       <artifactId>springfox-swagger-ui</artifactId>
       <version>2.9.2</version>
    </dependency>
    
    对于Gradle用户,在build.gradle文件中添加以下依赖项。
    
    implementation 'io.springfox:springfox-swagger2:2.9.2'
    implementation 'io.springfox:springfox-swagger-ui:2.9.2'
    
    现在,在主Spring Boot应用程序中添加@EnableSwagger2注解。@EnableSwagger2注释用于为您的Spring Boot应用程序启用Swagger2。
    主Spring Boot应用程序的代码如下所示-
    
    package com.jc2182.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @SpringBootApplication
    @EnableSwagger2
    public class SwaggerDemoApplication {
       public static void main(String[] args) {
          SpringApplication.run(SwaggerDemoApplication.class, args);
       }
    }
    
    接下来,创建Docket Bean为您的Spring Boot应用程序配置Swagger2。我们需要定义基本包以为Swagger2配置REST API。
    
            @Bean
            public Docket productApi() {
          return new Docket(DocumentationType.SWAGGER_2).select()
             .apis(RequestHandlerSelectors.basePackage("com.jc2182.demo")).build();
            }
    
    现在,将此bean添加到主Spring Boot应用程序类文件本身中,您的主Spring Boot应用程序类将如下所示
    
    package com.jc2182.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    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;
    
    @SpringBootApplication
    @EnableSwagger2
    public class SwaggerDemoApplication {
       public static void main(String[] args) {
          SpringApplication.run(SwaggerDemoApplication.class, args);
       }
       @Bean
       public Docket productApi() {
          return new Docket(DocumentationType.SWAGGER_2).select()
             .apis(RequestHandlerSelectors.basePackage("com.jc2182.demo")).build();
       }
    }
    
    现在,在构建配置文件中添加以下Spring Boot Starter Web依赖项以编写REST端点,如下所示-
    Maven用户可以在pom.xml文件中添加以下依赖项-
    
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    Gradle用户可以在build.gradle文件中添加以下依赖项-
    
    implementation 'org.springframework.boot:spring-boot-starter-web'
    
    现在,在此处显示在Rest Controller文件中构建两个简单的RESTful Web服务GET和POST的代码-
    
    package com.jc2182.demo;
    
    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
    public class SwaggerAPIController {
       @RequestMapping(value = "/products", method = RequestMethod.GET)
       public List<String> getProducts() {
          List<String> productsList = new ArrayList<>();
          productsList.add("Honey");
          productsList.add("Almond");
          return productsList;
       }
       @RequestMapping(value = "/products", method = RequestMethod.POST)
       public String createProduct() {
          return "Product is saved successfully";
       }
    }
    
    完整的构建配置文件在下面给出
    
    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
            <modelVersion>4.0.0</modelVersion>
            <parent>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-parent</artifactId>
                    <version>2.3.0.BUILD-SNAPSHOT</version>
                    <relativePath/> <!-- lookup parent from repository -->
            </parent>
            <groupId>com.jc2182</groupId>
            <artifactId>demo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <!-- <packaging>war</packaging> -->
            <name>demo</name>
            <description>Demo project for Spring Boot</description>
    
            <properties>
                    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <start-class>com.jc2182.demo.DemoApplication</start-class>
            </properties>
    
            <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.9.2</version>
                    </dependency>
                    <dependency>
                       <groupId>io.springfox</groupId>
                       <artifactId>springfox-swagger-ui</artifactId>
                       <version>2.9.2</version>
                    </dependency>
    
                    <dependency>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter-test</artifactId>
                            <scope>test</scope>
                            <exclusions>
                                    <exclusion>
                                            <groupId>org.junit.vintage</groupId>
                                            <artifactId>junit-vintage-engine</artifactId>
                                    </exclusion>
                            </exclusions>
                    </dependency>
    
            </dependencies>
    
            <build>
                    <plugins>
                            <plugin>
                                    <groupId>org.springframework.boot</groupId>
                                    <artifactId>spring-boot-maven-plugin</artifactId>
                            </plugin>
                    </plugins>
            </build>
    
            <repositories>
                    <repository>
                            <id>spring-milestones</id>
                            <name>Spring Milestones</name>
                            <url>https://repo.spring.io/milestone</url>
                    </repository>
                    <repository>
                            <id>spring-snapshots</id>
                            <name>Spring Snapshots</name>
                            <url>https://repo.spring.io/snapshot</url>
                            <snapshots>
                                    <enabled>true</enabled>
                            </snapshots>
                    </repository>
            </repositories>
            <pluginRepositories>
                    <pluginRepository>
                            <id>spring-milestones</id>
                            <name>Spring Milestones</name>
                            <url>https://repo.spring.io/milestone</url>
                    </pluginRepository>
                    <pluginRepository>
                            <id>spring-snapshots</id>
                            <name>Spring Snapshots</name>
                            <url>https://repo.spring.io/snapshot</url>
                            <snapshots>
                                    <enabled>true</enabled>
                            </snapshots>
                    </pluginRepository>
            </pluginRepositories>
    
    </project>
    
    Gradle – build.gradle
    
    plugins {
        id 'org.springframework.boot' version '2.3.0.BUILD-SNAPSHOT'
        id 'io.spring.dependency-management' version '1.0.9.RELEASE'
        id 'java'
    }
    
    group = 'com.jc2182'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '1.8'
    
    repositories {
        mavenCentral()
        maven { url 'https://repo.spring.io/milestone' }
        maven { url 'https://repo.spring.io/snapshot' }
    }
    
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-web' 
        implementation 'io.springfox:springfox-swagger2:2.9.2'
            implementation 'io.springfox:springfox-swagger-ui:2.9.2'
        testImplementation('org.springframework.boot:spring-boot-starter-test') {
            exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
        }
    }
    
    test {
        useJUnitPlatform()
    }    
    
    您可以创建一个可执行的JAR文件,并使用Maven或Gradle命令运行Spring Boot应用程序-
    对于Maven,您可以使用以下命令-
    
    mvn clean install
    
    在“BUILD SUCCESS”之后,您可以在target目录下找到JAR文件。
    对于Gradle,您可以使用以下命令-
    
    gradle clean build
    
    在“BUILD SUCCESSFUL”之后,您可以在build/libs目录下找到JAR文件。
    您可以使用以下命令运行JAR文件-
    
    java –jar <JARFILE>
    
    现在,该应用程序已使用https在Tomcat端口8080上启动,如下所示-
    springboot swagger
    现在,在Web浏览器中访问URL - http://localhost:8080/swagger-ui.html,然后查看Swagger API功能。
    springboot swagger