接口测试、管理神器-Swagger

简介: 接口测试、管理神器-Swagger

**Swagger**

1) 号称世界上最流行的API框架。

2) RestFul API文档在线自动生成工具->Api文档与API定义同步更新。

3) 直接运行,可以在线测试API接口。


[swagger_demo(gitee),记得给个小星星哈!](https://gitee.com/xi_jing/Swagger_demo1)

[swagger_demo(github),记得给个小星星哈!](https://github.com/hnust-xijing/Swagger_demo1)


[swagger官网地址](https://swagger.io/)



在项目中使用Swagger需要springbox;

1) swagger2

2) ui


**SpringBoot集成Swagger**

1,新建一个springBoot Web项目

2,导入相关依赖


```xml

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->

<dependency>

   <groupId>io.springfox</groupId>

   <artifactId>springfox-swagger-ui</artifactId>

   <version>2.10.5</version>

</dependency>




<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->

<dependency>

   <groupId>io.springfox</groupId>

   <artifactId>springfox-swagger2</artifactId>

   <version>2.10.5</version>

</dependency>

```


3,编写一个Hello工程。

4,编写Swagger==>Config


```java

@Configuration

@EnableSwagger2  //开启Swagger2

public class SwaggerConfig {

}

```


5,测试运行


```java

http://localhost:8080/swagger-ui.html#/hello-controller

```

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20201002130056190.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTY5NjQz,size_16,color_FFFFFF,t_70#pic_center)



**配置Swagger**

Swagger的bean实例Docket;


```java

package com.shuang.config;


import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

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;


import java.util.ArrayList;


@Configuration

@EnableSwagger2 //开启Swagger2

public class SwaggerConfig {


   //配置了swagger的docket的bean实例

   @Bean

   public Docket docket(){

       return new Docket(DocumentationType.SWAGGER_2)

               .apiInfo(apiInfo());

   }


   //配置swagger信息apiInfo

   private ApiInfo apiInfo() {


       Contact contack=new Contact("江爽","https://www.shishuangzhi.xyz","2894247242@qq.com");


       return new ApiInfo(

               "爽宝的Swagger API文档",

               "看到这个demo的人,能教我追妹子吗,有偿,微信号:js13617293003",

               "1.0",

               "https://www.shishuangzhi.xyz",

               contack,

               "Apache 2.0",

               "http://www.apache.org/license/LICENSE-2.0",

                new ArrayList()

       );

   }



}

```

**Swagger配置扫描接口**

Docket.select()


```java

//配置了swagger的docket的bean实例

@Bean

public Docket docket(){

   return new Docket(DocumentationType.SWAGGER_2)

           .apiInfo(apiInfo())

           .select()

           //RequestHandlerSelectors,配置要扫描接口的方式

           //basePackage:指定要扫描的包

           //any():扫描全部

           //none():不扫描

           //withclassAnnotation:扫描类上的注解

           .apis(RequestHandlerSelectors.basePackage("com.shuang.controller"))

           //paths(),过滤什么路径

           .paths(PathSelectors.ant("/shuang/**"))

           .build();

}

```


我只希望我的Swagger在生产环境中使用,在发布的时候不使用?

1)判断是不是生产环境 flag=false

2)注入enable(flag)


```java

//配置了swagger的docket的bean实例

//core.env结尾的

@Bean

public Docket docket(Environment environment){


   //设置要显示的Swagger环境

   Profiles profiles=Profiles.of("dev","test");


   //获取项目的环境

   //通过environment.acceptsProfiles判断是否处于自己设置的环境中。

   boolean flag = environment.acceptsProfiles(profiles);



   return new Docket(DocumentationType.SWAGGER_2)

           .apiInfo(apiInfo())

           .enable(flag)//enable是否启动Swagger,如果为false,则Swagger不能在浏览器中访问。

           .select()

           //RequestHandlerSelectors,配置要扫描接口的方式

           //basePackage:指定要扫描的包

           //any():扫描全部

           //none():不扫描

           //withclassAnnotation:扫描类上的注解

           .apis(RequestHandlerSelectors.basePackage("com.shuang.controller"))

           //paths(),过滤什么路径

           .paths(PathSelectors.ant("/shuang/**"))

           .build();

}

```


**配置API文档的分组**

.groupName("爽宝")


如何配置多个分组;多个Docket实例即可。


```java

@Bean

public Docket docket1(){

   return new Docket(DocumentationType.SWAGGER_2).groupName("A");

}

@Bean

public Docket docket2(){

   return new Docket(DocumentationType.SWAGGER_2).groupName("B");

}

@Bean

public Docket docket3(){

   return new Docket(DocumentationType.SWAGGER_2).groupName("C");

}

@Bean

public Docket docket4(){

   return new Docket(DocumentationType.SWAGGER_2).groupName("D");

}

```



![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20201002130119534.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTY5NjQz,size_16,color_FFFFFF,t_70#pic_center)



实体类


```java

package com.shuang.pojo;


import io.swagger.annotations.ApiModel;

import io.swagger.annotations.ApiModelProperty;


@ApiModel("用户实体类")

public class User {


   @ApiModelProperty("用户名")

   public String username;


   @ApiModelProperty("密码")

   public String password;

}

```


controller


```java

package com.shuang.controller;


import com.shuang.pojo.User;

import io.swagger.annotations.ApiOperation;

import io.swagger.annotations.ApiParam;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;


@RestController

public class HelloController {


   @GetMapping("/hello")

   public String hello(){

       return "hello";

   }


   //主要我们的接口中,返回值中存在实体类,他就会被扫描到Swagger中。

   @PostMapping("/user")

   public User user(){

       return new User();

   }

   //Operation接口,不是放在类上的,是方法

   @ApiOperation("Hello控制类")

   @GetMapping("/hello2")

   public String hello2(@ApiParam("用户名") String username){

       return "hello"+username;


   }


   @ApiOperation("post测试类")

   @PostMapping("/postt")

   public User hello3(@ApiParam("用户名") User user){

       return user;


   }

}

```


总结:

1,我们可以通过Swagger给一些比较难理解的属性或者接口,增加注释信息。

2,接口文档实时更新。

3,可以在线测试。

Swagger是一个优秀的工具,几乎所有大公司都有使用它。

【注意点】在正式发布的时候,关闭Swagger!!出于安全,也节约内存。













目录
相关文章
|
8天前
|
移动开发 JSON Java
Jmeter实现WebSocket协议的接口测试方法
WebSocket协议是HTML5的一种新协议,实现了浏览器与服务器之间的全双工通信。通过简单的握手动作,双方可直接传输数据。其优势包括极小的头部开销和服务器推送功能。使用JMeter进行WebSocket接口和性能测试时,需安装特定插件并配置相关参数,如服务器地址、端口号等,还可通过CSV文件实现参数化,以满足不同测试需求。
53 7
Jmeter实现WebSocket协议的接口测试方法
|
8天前
|
JSON 移动开发 监控
快速上手|HTTP 接口功能自动化测试
HTTP接口功能测试对于确保Web应用和H5应用的数据正确性至关重要。这类测试主要针对后台HTTP接口,通过构造不同参数输入值并获取JSON格式的输出结果来进行验证。HTTP协议基于TCP连接,包括请求与响应模式。请求由请求行、消息报头和请求正文组成,响应则包含状态行、消息报头及响应正文。常用的请求方法有GET、POST等,而响应状态码如2xx代表成功。测试过程使用Python语言和pycurl模块调用接口,并通过断言机制比对实际与预期结果,确保功能正确性。
35 3
快速上手|HTTP 接口功能自动化测试
|
8天前
|
JavaScript 前端开发 测试技术
ChatGPT与接口测试
ChatGPT与接口测试,测试通过
21 5
|
24天前
|
网络协议 测试技术 网络安全
Python进行Socket接口测试的实现
在现代软件开发中,网络通信是不可或缺的一部分。无论是传输数据、获取信息还是实现实时通讯,都离不开可靠的网络连接和有效的数据交换机制。而在网络编程的基础中,Socket(套接字)技术扮演了重要角色。 Socket 允许计算机上的程序通过网络进行通信,它是网络通信的基础。Python 提供了强大且易于使用的 socket 模块,使开发者能够轻松地创建客户端和服务器应用,实现数据传输和交互。 本文将深入探讨如何利用 Python 编程语言来进行 Socket 接口测试。我们将从基础概念开始介绍,逐步引导大家掌握创建、测试和优化 socket 接口的关键技能。希望本文可以给大家的工作带来一些帮助~
|
27天前
|
网络协议 测试技术 网络安全
Python进行Socket接口测试的实现
在现代软件开发中,网络通信是不可或缺的一部分。无论是传输数据、获取信息还是实现实时通讯,都离不开可靠的网络连接和有效的数据交换机制。而在网络编程的基础中,Socket(套接字)技术扮演了重要角色。 Socket 允许计算机上的程序通过网络进行通信,它是网络通信的基础。Python 提供了强大且易于使用的 socket 模块,使开发者能够轻松地创建客户端和服务器应用,实现数据传输和交互。 本文将深入探讨如何利用 Python 编程语言来进行 Socket 接口测试。我们将从基础概念开始介绍,逐步引导大家掌握创建、测试和优化 socket 接口的关键技能。希望本文可以给大家的工作带来一些帮助~
|
27天前
|
SQL Java 测试技术
SpringBoot单元测试快速写法问题之PorkService 接口中的 getPork 方法的作用如何解决
SpringBoot单元测试快速写法问题之PorkService 接口中的 getPork 方法的作用如何解决
|
29天前
|
XML Web App开发 数据挖掘
Postman接口测试工具全解析:功能、脚本编写及优缺点探讨
文章详细分析了Postman接口测试工具的功能、脚本编写、使用场景以及优缺点,强调了其在接口自动化测试中的强大能力,同时指出了其在性能分析方面的不足,并建议根据项目需求和个人偏好选择合适的接口测试工具。
38 1
|
29天前
|
Web App开发 JSON 测试技术
精通Postman接口测试:关联技术与自动化实践指南
这篇文章详细介绍了如何使用Postman进行接口测试,包括关联技术、自动化实践,以及如何通过环境变量和全局变量解决接口之间的关联性问题。
31 0
精通Postman接口测试:关联技术与自动化实践指南
|
1月前
|
SQL 安全 测试技术
[go 面试] 接口测试的方法与技巧
[go 面试] 接口测试的方法与技巧
|
29天前
|
JSON jenkins 测试技术
Python接口自动化测试框架(工具篇)-- 接口测试工具HTTPRUNNER
本文介绍了Python接口自动化测试框架HTTPRunner,包括其安装、使用方法,并通过实际操作演示了如何利用HTTPRunner进行接口测试,同时还探讨了HTTPRunner作为接口自动化测试解决方案的可能性和实用性。
36 0