Springboot多模块项目,在公共模块整合Swagger2

2020-12-12  本文已影响0人  轻轻敲醒沉睡的心灵

上一篇提到了SpringBoot整合Swagger2,但现在微服务盛行,一个项目可能就要搭建许多微服务,于是就想着将swagger2放到公共模块中,到时候直接把包引入就能用。基本上和上一篇一样,只不过就是把 swagger2的开关 和 描述提取到了配置文件中,使得引入公共模块的项目能在application.yml中控制swagger。

1. 需要了解的一些知识

  1. Springboot整合Swagger2 - 简书 (jianshu.com)
  2. SpringBoot条件装配
  3. SpringBoot配置文件中的数据格式
  4. SpringBoot读取和使用配置文件中的数据

2. 1. 导包

<!--引入swagger -->
<!-- 排除springfox-swagger2 引入的swagger-annotations、swagger-models 1.5.20版本,手动引入1.5.21版本的jar。
因为在使用@ApiModelProperty注解在字段上时,如果字段的类型为Long或是int类型, 那么程序启动后,访问swagger-ui.html的页面,
程序会报错: java.lang.NumberFormatException: For input string: "" -->
      <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
            <exclusions>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-annotations</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-models</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.21</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.21</version>
        </dependency>

3. 配置

至此,公共模块中swagger配置这样的:

image.png
因为要在yml文件中配置swagger,所以在公共模块META-INF中加一些元数据:additional-spring-configuration-metadata.json,非必须的,我用的eclipse,不想看见黄色感叹号,还能有提示
{"properties": [
  {
    "name": "swagger.enable",
    "type": "java.lang.Boolean",
    "description": "是否开启swagger'"
  },
  {
    "name": "swagger.title",
    "type": "java.lang.String",
    "description": "文档标题'"
  },
  {
    "name": "swagger.version",
    "type": "java.lang.String",
    "description": "版本号'"
  },
  {
    "name": "swagger.description",
    "type": "java.lang.String",
    "description": "描述'"
  },
  {
    "name": "swagger.head-enable",
    "type": "java.lang.Boolean",
    "description": "是否开启head参数'"
  },
  {
    "name": "swagger.head-params",
    "type": "java.util.List",
    "description": "head参数'"
  },
  {
    "name": "swagger.base-package",
    "type": "java.util.List",
    "description": "api扫描包路径"
  }
]}

我放到这了:


image.png

配置,到这就完成了。

4. 使用

使用时,在其他微服务中导入公共模块的jar包,然后在yml文件中配置。

# 5.swagger
swagger: 
  enable: true
  title: ${spring.application.name}
  version: 1.0
  base-package: 
    - com.test.controller
  description: 访问地址:http://192.168.50.30:${server.port}${server.servlet.context-path}
  head-enable: true
  head-params:  
    - param: token1
      required: true
    - param: token2
      required: false

自己封装自己用,可以自嗨了。。。。。。。

上一篇下一篇

猜你喜欢

热点阅读