使用Swagger2自动测试
目录结构
源码
pom.xml依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加swagger2相关功能 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 添加swagger-ui相关功能 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 引入该依赖即可开启热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
GoodsController
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController // 通过该注解,第一是将GoodsController注册为控制器,可以响应Http请求;第二是可以将控制器中的方法返回值序列化为json格式。
public class GoodsController {
@Autowired // 自动装配goodsService
private GoodsService goodsService;
/**
* 查询商品信息
* 1、@GetMapping表示可以使用get方法请求该api
* 2、"/goods/{id}"表示请求路径为/goods/{id}的形式,其中{id}为占位符
* 3、@PathVariable("id")表示将占位符{id}的值传递给id
* 4、也就是说/goods/123请求的话,会将123传递给参数id
*/
@GetMapping("/goods/{id}")
public GoodsDo getOne(@PathVariable("id") long id) {
return goodsService.getGoodsById(id);
}
/**
* 查询商品列表,使用get方法
*/
@GetMapping("/goods")
public List<GoodsDo> getList() {
return goodsService.getGoodsList();
}
/**
* 新增商品
* 1、@PostMapping表示使用post方法
* 2、@RequestBody表示将请求中的json信息转换为GoodsDo类型的对象信息,该转换也是由SpringMVC自动完成的
*/
@PostMapping("/goods")
public void add(@RequestBody GoodsDo goods) {
goodsService.addGoods(goods);
}
/**
* 修改商品
*/
@PutMapping("/goods/{id}")
public void update(@PathVariable("id") long id, @RequestBody GoodsDo goods) {
// 修改指定id的商品信息
goods.setId(id);
goodsService.editGoods(goods);
}
/**
* 删除商品
*/
@DeleteMapping("/goods/{id}")
public void delete(@PathVariable("id") long id) {
goodsService.removeGoods(id);
}
}
Goods
/**
* 商品类
*/
public class GoodsDo {
/**
* 商品id
*/
private Long id;
/**
* 商品名称
*/
private String name;
/**
* 商品价格
*/
private String price;
/**
* 商品图片
*/
private String pic;
// 省略了setter以及getter方法
GoodsService
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* 商品服务
*/
@Service // 注册为服务类
public class GoodsService {
/**
* 获取商品列表
*/
public List<GoodsDo> getGoodsList() {
List<GoodsDo> goodsList = new ArrayList<GoodsDo>();
GoodsDo goods = new GoodsDo();
goods.setId(1L);
goods.setName("苹果");
goods.setPic("apple.jpg");
goods.setPrice("3.5");
goodsList.add(goods);
return goodsList;
}
/**
* 按id获取商品信息,模拟返回对应商品信息
*/
public GoodsDo getGoodsById(Long id) {
GoodsDo goods = new GoodsDo();
goods.setId(1L);
goods.setName("苹果");
goods.setPic("apple.jpg");
goods.setPrice("3.5");
return goods;
}
/**
* 新增商品,模拟返回数据库影响行数
*/
public int addGoods(GoodsDo goods) {
return 1;
}
/**
* 根据商品id更新商品信息,模拟返回数据库影响行数
*/
public int editGoods(GoodsDo goods) {
return 1;
}
/**
* 根据商品id删除对应商品,模拟返回数据库影响行数
*/
public int removeGoods(Long id) {
return 1;
}
}
Swagger2Config
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration // 告诉Spring容器,这个类是一个配置类
@EnableSwagger2 // 启用Swagger2功能
public class Swagger2Config {
/**
* 配置Swagger2相关的bean
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com"))// com包下所有API都交给Swagger2管理
.paths(PathSelectors.any()).build();
}
/**
* 此处主要是API文档页面显示信息
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("演示项目API") // 标题
.description("学习Swagger2的演示项目") // 描述
.termsOfServiceUrl("http://www.wl.com") // 服务网址,一般写公司地址
.version("1.0") // 版本
.build();
}
}
测试文档
@Api(tags = "商品API") // 类文档显示内容
@RestController
public class GoodsController {
@Autowired
private GoodsService goodsService;
@ApiOperation(value = "根据id获取商品信息") // 接口文档显示内容
@GetMapping("/goods/{id}")
public GoodsDo getOne(@PathVariable("id") long id) {
return goodsService.getGoodsById(id);
}
@ApiOperation(value = "获取商品列表") // 接口文档显示内容
@GetMapping("/goods")
public List<GoodsDo> getList() {
return goodsService.getGoodsList();
}
@ApiOperation(value = "新增商品") // 接口文档显示内容
@PostMapping("/goods")
public void add(@RequestBody GoodsDo goods) {
goodsService.addGoods(goods);
}
@ApiOperation(value = "根据id修改商品信息") // 接口文档显示内容
@PutMapping("/goods/{id}")
public void update(@PathVariable("id") long id, @RequestBody GoodsDo goods) {
goods.setId(id);
goodsService.editGoods(goods);
}
@ApiOperation(value = "根据id删除商品") // 接口文档显示内容
@DeleteMapping("/goods/{id}")
public void delete(@PathVariable("id") long id) {
goodsService.removeGoods(id);
}
}
效果
访问 http://127.0.0.1:8080/swagger-ui.html ,即可打开自动生成的可视化测试页面,
对于每个方法可点击 Try it out 开始测试