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

相关推荐
花间相见1 天前
【计算基础|网络04】—— HTTP接口实战(下):接口测试、鉴权与跨域排错
java·linux·人工智能·后端·python·计算机网络·postman
霍格沃兹测试学院-小舟畅学2 天前
UI 测试的语义断言:Midscene aiAssert 比像素比对稳在哪、又会骗在哪
人工智能·测试工具
zuozewei2 天前
附录 A:主流工具对比选型表
人工智能·测试工具
我的xiaodoujiao2 天前
Django 基础知识详细图文教程 10-Django 模板引擎 3
后端·python·测试工具·django
霍格沃兹测试学院-小舟畅学2 天前
Google 开源 ARTEMIS:AI Agent 如何接管 Android 真机测试?
人工智能·测试工具
Liaiyang662 天前
# 自研 AST 容错初筛工具:横向评测 Django、PyTorch、TensorFlow 三大开源 Python 框架异常收容风险
python·测试工具·自动化·开源软件·代码规范·devops·代码复审
霍格沃兹测试学院-小舟畅学2 天前
我用 AI 把 Swagger 变成 500 条 pytest 用例,回归从 3 天到 3 小时
人工智能·测试工具
奔跑中的小象3 天前
GuideLLM 压测实战指南:给大模型推理服务做一次完整 SLA 评估
测试工具·guidellm
Madison-No73 天前
基于JMeter工具的性能测试(接口)
python·jmeter·postman
cpolar技术支持3 天前
本地 Playwright 测试报告怎么远程复盘?Trace Viewer 跑起来后,用 cpolar 分享失败现场
前端·自动化测试·测试工具·cpolar·playwright