谷粒学苑项目实战(三):整合Swagger2

17 篇文章 3 订阅

目录

一、Swagger介绍

二、整合Swagger 

        1、创建common模块 

        2、引入相关依赖

         3、创建模块service_base

        4、创建SwaggerConfig 

        5、在service中引入service_base 

        6、在启动类上添加注解 

三、启动测试 

四、常见问题解决 


一、Swagger介绍

        前后端分离开发模式中,api文档是最好的沟通方式。
        Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。
        1. 及时性 (接口变更后,能够及时准确地通知相关前后端开发人员)
        2. 规范性 (并且保证接口的规范性,如接口的地址,请求方式,参数及响应格式和错误信息)
        3. 一致性 (接口信息一致,不会出现因开发人员拿到的文档版本不一致,而出现分歧)
        4. 可测性 (直接在接口文档上进行测试,以方便理解业务)

 

 

二、整合Swagger 

 

        1、创建common模块 

        在guli-parent下创建模块common ,用来存放一些公共使用的功能。

        2、引入相关依赖

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

    <artifactId>common</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>service_base</module>
        <module>common_utils</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>-->
    </dependencies>
</project>

 

         3、创建模块service_base

        在common下面创建子模块service-base,用来存放 service 模块中需要使用的功能

        4、创建SwaggerConfig 

          在模块service-base中,创建swagger的配置类SwaggerConfig ,然后 创建包com.shang.service_base.config,创建类SwaggerConfig  

        

@Configuration
@EnableSwagger2     //swagger注解
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();
    }

    private ApiInfo webApiInfo(){

        return new ApiInfoBuilder()
                .title("网站-课程中心API文档")
                .description("本文档描述了课程中心微服务接口定义")
                .version("1.0")
                .contact(new Contact("java", "http://atguigu.com", "1123@qq.com"))
                .build();
    }
}

        5、在service中引入service_base 

        在service的pom.xml文件中引入

<dependency>
            <groupId>com.shang</groupId>
            <artifactId>service_base</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        6、在启动类上添加注解 

        在service_edu的启动类上增加注解

@ComponentScan(basePackages = "com.shang")  //扫描swagger等配置类

三、启动测试 

        访问        http://localhost:8001/swagger-ui.html 

 

        看到这个窗口说明配置成功。

 

四、常见问题解决 

        假如访问后出现一个全是英文而且关不掉的弹窗,尝试排查:

        1、依赖是否引入失败 

        2、注解是否加上

        3、重启IDEA(我的注解和依赖都没有问题,就是通过重启IDEA解决的)

        4、刷新maven,重新导入依赖

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值