关于vue.js:SpringCloud微服务实战搭建企业级开发框架六使用knife4j集成Swagger2接口文档

33次阅读

共计 5500 个字符,预计需要花费 14 分钟才能阅读完成。

  knife4j 是为集成 Swagger 生成 api 文档的加强解决方案,前后端 Java 代码以及前端 Ui 模块进行拆散, 在微服务架构下应用更加灵便,
提供专一于 Swagger 的加强解决方案, 不同于只是改善加强前端 Ui 局部,咱们这里应用 knife4j 作为文档管理工具来代替 swagger-ui。

1、在 GitEgg-Platform 工程下新建 gitegg-platform-swagger 子工程,在 GigEgg-Platform 中的子工程 gitegg-platform-bom 中,批改 pom
.xml,以 maven bom 的形式应用 knife4j:

            <!-- Swagger2 knife4j bom 形式引入 -->
            <dependency>
                <groupId>com.github.xiaoymin</groupId>
                <artifactId>knife4j-dependencies</artifactId>
                <version>${knife4j.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

2、在 gitegg-platform-swagger 子工程中的 pom.xml 增加 knife4j 援用:

<?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">
    <parent>
        <artifactId>GitEgg-Platform</artifactId>
        <groupId>com.gitegg.platform</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>gitegg-platform-swagger</artifactId>
    <name>${project.artifactId}</name>
    <version>${project.parent.version}</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

</project>

3、在 gitegg-platform-swagger 子工程中新建 SwaggerConfig.java 文件:

package com.gitegg.platform.swagger.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig {@Bean(value = "GitEggApi")
    public Docket GitEggApi() {Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                // 分组名称
                .groupName("2.X 版本")
                .select()
                // 这里指定 Controller 扫描包门路
                .apis(RequestHandlerSelectors.basePackage("com.gitegg.*.*.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    private ApiInfo apiInfo() {return new ApiInfoBuilder().version("1.0.0")
                .title("Spring Cloud Swagger2 文档")
                .description("Spring Cloud Swagger2 文档")
                .termsOfServiceUrl("www.gitegg.com")
                .build();}

}

4、在 gitegg-service 工程中引入 gitegg-platform-swagger

<?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">
    <parent>
        <artifactId>GitEgg-Cloud</artifactId>
        <groupId>com.gitegg.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>gitegg-service</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>gitegg-service-base</module>
        <module>gitegg-service-bigdata</module>
        <module>gitegg-service-system</module>
    </modules>

    <dependencies>
        <!-- gitegg 数据库驱动及连接池 -->
        <dependency>
            <groupId>com.gitegg.platform</groupId>
            <artifactId>gitegg-platform-db</artifactId>
        </dependency>
        <!-- gitegg mybatis-plus -->
        <dependency>
            <groupId>com.gitegg.platform</groupId>
            <artifactId>gitegg-platform-mybatis</artifactId>
        </dependency>
        <!-- gitegg swagger2-knife4j -->
        <dependency>
            <groupId>com.gitegg.platform</groupId>
            <artifactId>gitegg-platform-swagger</artifactId>
        </dependency>
        <!-- spring boot web 外围包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- spring boot 衰弱监控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

</project>

5、在 gitegg-service-system 工程下的 SystemController.java 类外面增加 Swagger2 相干注解

package com.gitegg.service.system.controller;

import com.gitegg.service.system.service.ISystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "system")
@AllArgsConstructor
@Api(tags = "gitegg-system")
public class SystemController {

    private final ISystemService systemService;

    @GetMapping(value = "list")
    @ApiOperation(value = "system list 接口")
    public Object list() {return systemService.list();
    }


    @GetMapping(value = "page")
    @ApiOperation(value = "system page 接口")
    public Object page() {return systemService.page();
    }
}

6、GitEggSystemApplication.java 退出组件扫描的注解,让 Spring 在启动的时候加载到 swagger2 的配置:

package com.gitegg.service.system;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * gitegg-system 启动类
 */
@ComponentScan(basePackages = "com.gitegg")
@MapperScan("com.gitegg.*.*.mapper")
@SpringBootApplication
public class GitEggSystemApplication {public static void main(String[] args) {SpringApplication.run(GitEggSystemApplication.class,args);
    }
}

7、运行 gitegg-service-system,关上浏览器拜访:http://127.0.0.1:8001/doc.html,能够看到 swagger2 文档界面

本文源码在 https://gitee.com/wmz1930/GitEgg 的 chapter-06 分支。

正文完
 0