多模块整合Swagger(springboot,maven)

  • Post author:
  • Post category:其他




项目目录结构

在这里插入图片描述


父项目lmh_parent,公共使用的子模块commons,业务子模块service,等将Swagger整合到子模块commons下的子子模块servicebase



依赖


commons

<?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>lmh_parent</artifactId>
        <groupId>com.lmh</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>commons</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>servicebase</module>
    </modules>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <scope>provided</scope>
        </dependency>

        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <scope>provided</scope>
        </dependency>

        <!--lombok用来简化实体类:需要安装lombok插件-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <!-- spring2.X集成redis所需common-pool2-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.6.0</version>
        </dependency>

        <!-- JWT-->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
        </dependency>
    </dependencies>


servicebase

<?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>commons</artifactId>
        <groupId>com.lmh</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>servicebase</artifactId>


</project>



创建一个SwaggerConfig类,路径也与项目启动类的有有共同的包名路径

package com.lmh.servicebase;

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2//开启swagger注解
@Configuration
public class SwaggerConfig{

    @Bean
    public Docket webApiConfig(){

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")//自定义名称
                .apiInfo(webApiInfo())
                .select()
                //.paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();

    }
//自定义编写页面内容,下面内容会显示带Swagger-ui.html上
    private ApiInfo webApiInfo(){

        return new ApiInfoBuilder()
                .title("swagger")
                .description("测试请求")
                .version("1.0")
                .contact(new Contact("magic林", "http://lmh.com", "123456@qq.com"))
                .build();
    }
    //整合成功后
    //http://localhost:8001/swagger-ui.html访问结果如上面设置得信息
}



问题:该配置类怎么被加载


解决

:1.在业务子模块有项目启动类的pom.xml文件引入依赖



service子模块

引入

<dependency>
            <groupId>com.lmh</groupId>
            <artifactId>servicebase</artifactId>
            <version>0.0.1-SNAPSHOT</version>

        </dependency>

依赖不唯一,取决于

自己定义时的名称版本


2.在项目启动类(主程序)上开启注解扫描

package com.lmh.eduService;

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

@SpringBootApplication
@ComponentScan(basePackages = {"com.lmh"})//扫描其他模块的配置类,有com.lmh路径相同都可以
public class EduApplication {
    public static void main(String[] args) {
        SpringApplication.run(EduApplication.class,args);
    }
}

其中SwaggerConfig的包名路径包括

com.lmh

,则可以加载com.lmh包下的所有配置,前提是依赖要引入正确



启动项目,访问http://localhost:8001/swagger-ui.html


端口取决与个人配置的server.port


在这里插入图片描述



版权声明:本文为weixin_44884861原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。