整合Swagger2进行接口测试


优点:1、生成在线接口文档

           2、方便接口测试

通常建一个common的maven模块,然后导入swagger相关依赖

<packaging>pom</packaging>
<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>

在common模块中,添加一个模块service_base对swagger进行整合

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("leavescai7", "http://leavescai7", "384687690@qq.com"))
                .build();
    }
}

具体使用swagger:

首先在需要使用的地方pom文件中引入依赖、

在service_edu中引入service_base

 

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

 

还需要在service_edu的启动类中加一个扫描注解,才可以调用swagger的配置类。设置扫描规则

启动类中加入:

@ComponentScan(basePackages = {"com.leaves"})

最后,访问swageer:

 

 

用swagger进行方法测试:

 

 

 

 

 测试成功!!!

对于swagger完美加api注释:

类: @Api(description="讲师管理")
方法:@ApiOperation(value = "所有讲师列表")
参数:@ApiParam(name = "id", value = "讲师ID", required = true)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM