使⽤Swagger2⾃动测试--参考慕课熊猫
使⽤Swagger2⾃动测试
⽬录结构
源码
<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) {
GoodsById(id);
}
/
**
* 查询商品列表,使⽤get⽅法
*/
@GetMapping("/goods")
public List<GoodsDo> getList() {
GoodsList();
}
/**
* 新增商品
* 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) {
}
}
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 t.annotation.Bean;
import t.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容器,这个类是⼀个配置类
spring framework documentation@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("www.wl") // 服务⽹址,⼀般写
.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) {
GoodsById(id);
}
@ApiOperation(value = "获取商品列表") // 接⼝⽂档显⽰内容
@GetMapping("/goods")
public List<GoodsDo> getList() {
GoodsList();
}
@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) {
}
}
效果

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。