接口测试工具的实验,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 即可

相关推荐
辣香牛肉面4 小时前
Wireshark v4.6.2 开源免费网络嗅探抓包工具中文便携版
网络·测试工具·wireshark
may_一一21 小时前
xpath定位:selenium和playwrightAnt Design / 表单类页面)
selenium·测试工具
daopuyun1 天前
CNAS/CMA软件检测实验室源代码漏洞测试工具选型要求与比对
软件测试·测试工具·软件检测·cnas认可·cma认定
Wpa.wk1 天前
接口自动化测试 - 请求构造和响应断言 -Rest-assure
开发语言·python·测试工具·接口自动化
AI_56781 天前
Postman接口测试提速技巧:批量请求+智能断言实践
测试工具·lua·postman
Luminbox紫创测控1 天前
整车自然暴晒与全光谱阳光模拟老化相关性研究
测试工具
Warren981 天前
Pytest Fixture 作用域与接口测试 Token 污染问题实战解析
功能测试·面试·单元测试·集成测试·pytest·postman·模块测试
弹简特1 天前
【JavaEE06-后端部分】SpringMVC01-Spring MVC第一大核心URL 路由映射【建立连接】与 Postman 接口测试详解
java·spring boot·测试工具·spring·postman
测试大圣2 天前
软件测试基础知识总结(超全的)
软件测试·python·功能测试·测试工具·职场和发展·单元测试·测试用例