多模块整合Swagger(springboot,maven)

3 篇文章 0 订阅
1 篇文章 0 订阅

项目目录结构

在这里插入图片描述
父项目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
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Swagger是一种API文档生成工具,可以帮助开发人员快速生成API文档并提供可视化的界面。而Spring Boot是一种快速开发框架,可以帮助开发人员快速搭建基于Spring的应用程序。结合使用SwaggerSpring Boot可以更加方便地生成API文档,并且可以在开发过程中快速测试和调试API接口。 ### 回答2: Swagger是一组用于设计、构建和文档化RESTful Web服务的API开发工具。Swagger提供了一个开放的标准,它在API设计中采用了RESTful原则,使得API接口的设计更为简单易懂,并支持自动化的API文档生成。 Spring Boot是一款开发Web应用程序和微服务的Java框架。借助Spring Boot,开发人员可以构建自包含、嵌入式的Web服务器,并快速构建、运行、测试和部署应用程序。Spring Boot开箱即用,提供了许多常用Web开发功能的自动配置。它减少了开发人员在应用程序开发过程中所需编写的样板代码,从而提高了应用程序的开发效率。 将SwaggerSpring Boot结合使用,可以更加简便地创建和部署RESTful Web服务,同时帮助API文档自动化生成。Spring Boot提供了一些用于集成Swagger的一些方便配置,例如,Springfox和Swagger2依赖。借助这个集成工具,开发人员可以在开发Web应用程序时编写API,而不必离开IDE (Integrated Development Environment)。同时,开发人员也可以轻松地生成API文档,并固化在程序中,以便程序更容易地交互和通信。固化API文档也为API的测试和维护提供很多便利。 因此,Swagger Spring Boot是一种非常有用的工具,它精简了开发人员日常工作流程中的许多步骤,包括API的设计、文档化和测试。它可以帮助开发人员快速、高效地构建复杂的Web应用程序和微服务。 ### 回答3: SwaggerSpring Boot是用于开发RESTful Web服务的常用框架和工具。Swagger是一种API文档工具,用于描述RESTful Web服务的API,并提供了一些有用的功能,例如API的测试、debug和文档的生成。Spring Boot是一个快速开发框架,能快速创建一个可运行的、生产级别的基于Spring的应用程序。Spring Boot提供了许多开箱即用的场景,减少了开发人员的工作量。 SwaggerSpring Boot的结合可以使开发人员更加快捷地创建RESTful Web服务。通过在Spring Boot应用程序中集成Swagger,开发人员可以自动生成RESTful API的文档和客户端代码。Swagger UI是一种Java库,可以自动将RESTful API文档渲染为漂亮的、交互式的GUI。开发人员可以使用Swagger UI浏览RESTful API的文档,并测试API的各个端点是否正常工作。 在使用Spring BootSwagger时,开发人员需要配置Swagger注解和插件来描述API。通过在Spring BootMaven或Gradle插件中添加Swagger插件,可以生成Swagger文档和客户端代码。开发人员可以使用标记注解来提供额外的信息,例如HTTP方法,参数,请求体和响应体等,以帮助生成更准确的API文档。 总之,SwaggerSpring Boot就是一对强悍的组合,能够帮助开发人员更快地创建RESTful Web服务,同时提高API的可读性和可测试性,方便开发和维护。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值