目前市面上存在很多接口测试的工具,jmeter,postman,swagger等等(我目前就使用过这几种)。swagger是最后才接触的,当我接触到swagger之后我觉得swagger更适合我自己的使用范围(另外两种可能对测试人员或者其他人来说更适合)。springboot能够快速的集成swagger。话不多说,直接来看集成步骤:
1. 在pom.xml文件中添加swagger2依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2. 添加Swagger2配置
Swagger2.java
package com.example.demo001;
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;
/**
* @author
* @date 2018/11/6 16:36
* @description
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
/**
* swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
*
* @return
*/
@Bean
public Docket createRestApi() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//包路径
.apis(RequestHandlerSelectors.basePackage("com.example.demo001"))
.paths(PathSelectors.any())
.build();
return docket;
}
/**
* 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
*
* @return
*/
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfoBuilder()
//页面标题
.title("测试Swagger")
//创建人
.contact(new Contact("张三丰", "http://www.baidu.com", "zhangsanfeng@123.com"))
//版本号
.version("1.0")
//描述
.description("API 描述")
.build();
return apiInfo;
}
}
3. 在Demo001Cntroller中添加相关注注解
代码如下
package com.example.demo001.controller;
import com.example.demo001.dto.OrderDetail;
import com.example.demo001.service.Demo001Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author
* @date 2018/11/5 20:41
* @description
*/
@Api("测试--Swagger说明")
@RestController
@RequestMapping("/demo")
public class Demo001Cntroller {
@Autowired
private Demo001Service demo001Service;
//接口说明
@ApiOperation(value = "查询订单详情--根据ID查询", notes = "查询某个订单详情")
//参数说明
//paramType的取值及含义如下
//path --> /users/{id}
//query --> /users?role=admin
//header --> X-MyHeader: Value
//cookie --> Cookie: debug=0; csrftoken=BUSe35dohU3O1MZvDCU
@ApiImplicitParam(name = "detailId", value = "订单详情ID", paramType = "query", required = true, dataType = "String")
@RequestMapping(value = "/getOrderDetail", method = {RequestMethod.GET, RequestMethod.POST})
public OrderDetail getOrderDetail(@RequestParam("detailId") String detailId) {
return demo001Service.getOrderDetailById(detailId);
}
}
4. 测试
启动服务在浏览器中输入:http://localhost:8080/swagger-ui.html
点击 Try it out
输入接口参数
点击Execute
查看接口返回结果
到此完成