使用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 開始測試