接口测试、管理神器-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!!出于安全,也节约内存。













目录
相关文章
|
1月前
|
API
支付系统38-----支付宝支付---统一收单线下交易查询 第一步下单------》发起支付请求,登录,确认支付,查单接口开发,swagger接口全部呈现,
支付系统38-----支付宝支付---统一收单线下交易查询 第一步下单------》发起支付请求,登录,确认支付,查单接口开发,swagger接口全部呈现,
|
23天前
|
存储
Postman 接口测试配置 Pre-request Script
Postman 接口测试配置 Pre-request Script
66 5
Postman 接口测试配置 Pre-request Script
|
12天前
|
SQL 安全 测试技术
[go 面试] 接口测试的方法与技巧
[go 面试] 接口测试的方法与技巧
|
1月前
|
XML JSON 测试技术
Postman接口测试工具详解
📚 Postman全攻略:API测试神器!📚 发送HTTP请求,管理集合,写测试脚本,集成CI/CD。从安装配置到环境变量、断言、数据驱动测试,一步步教你如何高效测试RESTful API。实战案例包含GET、POST、PUT、DELETE请求。用Newman在命令行跑集合,自动化测试不发愁!👉 [洛秋小站](https://www.luoqiu.site/) 学更多!🚀
61 1
|
21天前
|
JSON 前端开发 测试技术
Postman 接口测试工具详解
在执行这些测试案例时,请确保遵循实际的API规范,并根据API的特定要求调整步骤和参数。
|
1月前
|
数据采集 测试技术
常见测试测量接口的比较:PXI、PXIe、PCI、VXI、GPIB、USB
常见测试测量接口的比较:PXI、PXIe、PCI、VXI、GPIB、USB
49 2
|
24天前
|
Java 测试技术
JMeter接口性能测试使用
JMeter接口性能测试使用
26 0
|
1月前
|
JSON 测试技术 数据格式
postman接口测试工具详解
postman接口测试工具详解
|
1月前
|
JSON 数据格式
MysbatisPlus-核心功能-IService开发基础业务接口,MysbatisPlus_Restful风格,新增@RequestBody指定是为了接收Json数据的,使用swagger必须注解
MysbatisPlus-核心功能-IService开发基础业务接口,MysbatisPlus_Restful风格,新增@RequestBody指定是为了接收Json数据的,使用swagger必须注解
支付系统---微信支付14----创建案例项目---介绍,第二步引入Swagger,接口文档和测试页面生成工具,定义统一结果的目的是让结果变得更加规范,以上就是谷粒项目的几个过程
支付系统---微信支付14----创建案例项目---介绍,第二步引入Swagger,接口文档和测试页面生成工具,定义统一结果的目的是让结果变得更加规范,以上就是谷粒项目的几个过程

热门文章

最新文章