接口测试工具的实验,Postman、Swagger、knife4j(黑马头条)

一、Postman

最常用的接口测试软件,需要注意点:在进行post请求时,需要选择JSON形式发送

输入JSON字符串,比如:

复制代码
{
	"maxBehotTime": "2021-04-19 00:19:09",
	"minBehotTime": "2021-04-10 00:19:09",
	"size": 10,
	"tag": "Java"
}

二、Swagger

1.Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务(https://swagger.io/)。

2.实现过程:

(1)导包:

复制代码
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
</dependency>

(2)添加自动配置类SwaggerConfiguration

复制代码
package com.heima.common.swagger;

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.builders.RequestHandlerSelectors;
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 SwaggerConfiguration {

   @Bean
   public Docket buildDocket() {
      return new Docket(DocumentationType.SWAGGER_2)
              .apiInfo(buildApiInfo())
              .select()
              // 要扫描的API(Controller)基础包
              .apis(RequestHandlerSelectors.basePackage("com.heima"))
              .paths(PathSelectors.any())
              .build();
   }

   private ApiInfo buildApiInfo() {
      Contact contact = new Contact("黑马程序员","","");
      return new ApiInfoBuilder()
              .title("黑马头条-平台管理API文档")
              .description("黑马头条后台api")
              .contact(contact)
              .version("1.0.0").build();
   }
}

(3)在spring.factories添加这个类的全路径 使其生效 ()

复制代码
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.heima.common.exception.ExceptionCatch,\
  com.heima.common.swagger.SwaggerConfiguration,\
  com.heima.common.swagger.Swagger2Configuration

(4)然后在相关的controller中添加注解@Api,方法上可以添加@ApiOperation,对象加入@ApiModelProperty。

ApUserLoginController:

LoginDto:

(5)看一下自己bootstrap.yml的访问地址端口号,我的是localhost:51801,结果如下:

(6)最后,访问地址:http://localhost:51801/swagger-ui.html (注意端口号)

三、knife4j

1.比swagger多几个功能,包括生成离线文档、个性化配置等。

2.配置基本同swagger

(1)导包

复制代码
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
</dependency>

(2)添加自动配置类Swagger2Configuration

java 复制代码
package com.heima.common.swagger;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableKnife4j    // 该注解是`knife4j`提供的增强注解,Ui提供了例如动态参数、参数过滤、接口排序等增强功能
@Import(BeanValidatorPluginsConfiguration.class)
public class Swagger2Configuration {

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //分组名称
                .groupName("1.0")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.heima"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("黑马头条API文档")
                .description("黑马头条API文档")
                .version("1.0")
                .build();
    }
}

(3)(4)(5)同swagger

最后输入:http://localhost:51801/doc.html 即可

相关推荐
测试199820 小时前
如何学习自动化测试?
自动化测试·软件测试·python·学习·测试工具·职场和发展·测试用例
卓码软件测评21 小时前
CNAS软件测试机构:【Postman集合从接口组织到自动化测试套件的过程】
网络·测试工具·性能优化·测试用例·压力测试·postman
测试人社区—52721 天前
无需修改测试用例实现Selenium四倍性能提升的完整方案
selenium·测试工具·测试用例
谷粒.1 天前
Cypress vs Playwright vs Selenium:现代Web自动化测试框架深度评测
java·前端·网络·人工智能·python·selenium·测试工具
斯南1 天前
wireshark的基本使用,过滤请求,过滤数据,追踪数据
网络·测试工具·wireshark
十二测试录1 天前
用F12获取接口信息,并进行接口测试
经验分享·功能测试·测试工具·压力测试·职场发展·安全性测试
一念一花一世界1 天前
PostIn零基础速成:从安装到入门使用
postman·接口管理工具
卓码软件测评2 天前
第三方软件测试评测机构:【基于Scala DSL的Gatling脚本开发:从零开始构建首个负载测试模型】
后端·测试工具·测试用例·scala·负载均衡·压力测试
川石课堂软件测试2 天前
自动化测试的基本概念及常用框架
数据库·python·功能测试·测试工具·单元测试·自动化·流程图
测试19982 天前
简单的Web UI自动化测试框架Java实现
自动化测试·软件测试·selenium·测试工具·ui·职场和发展·测试用例