整合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