自动化接口测试第二页 swagger插件

swagger插件结合spring boot非常好用,spring boot开发好接口后添加上swagger插件就可以在页面上看到接口信息,也可以直接测试接口,不管自己用还是给同事看接口都是极好的

1 在这里使用gradle添加swagger依赖

    compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.6.1'
    compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.6.1'

2 添加swagger配置代码

package com.example.server;

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;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).pathMapping("/").select()
                .paths(PathSelectors.regex("/.*")).build();
    }

    private ApiInfo apiInfo() {
        // TODO Auto-generated method stub
        return new ApiInfoBuilder().title("我的接口文档").contact(new Contact("dazou", "", "[email protected]")).description("这是我的swergerUI生成的接口文档").version("1.0.0.0").build();
    }

}
如图:

自动化接口测试第二页 swagger插件

3 为各接口添加swagger标识,如图:

自动化接口测试第二页 swagger插件

4 都弄完了,程序跑起来,浏览器输入:localhost:8888/swagger-ui.html  查看