knife4j集合化postman
01 knife4j的介绍
- 基于 JavaMVC的集成框架swagger的进一步强化,在原有通过注释就能生成文档的前身swagger-bootstrap-ui之上,增加了postman的测试功能,优化了文档的UI界面,在测试api接口的方面有了极大的进步
02 前期准备
1.引入依赖
xml
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.9</version>
</dependency>
2.配置yml文件
yml
server:
port: 8080
servlet:
context-path: /web
# 配置数据源
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
3.引入配置类
java
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))//扫描的包路径
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("接口文档的标题")//文档标题
.version("1.0.0")//文档版本说明
.description("文档的描述")//文档的描述
.build();
}
}
03 测试
1.测试的实体类
Java
@ApiModel("类别实体类")//用于标记实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Category {
@ApiModelProperty("类别编码")//用于标记实体类属性作用
private Long categoryId;
private String categoryName;
private String categoryPicture1;
private String categoryPicture2;
}
2.测试的控制类
java
@Api(tags = "类别模块")//用于标记整个控制模块
@RestController
@RequestMapping("/category")
public class CategoryController {
//用于标记该路径,value是标记该路径名,notes是详细的解释
@ApiOperation(value = "查询类别",notes = "根据类别id查询类别" )
@GetMapping("/select")
public Category select(Long categoryId){
Category category = new Category();
return category;
}
@ApiImplicitParam(name = "categoryId",value = "类别编号",required = true)//用于标记返回值,同样的,name是标记名字,value是解释
@PostMapping("/post")
public Category post(Long categoryId){
Category category = new Category();
return category;
}
@ApiImplicitParams({
@ApiImplicitParam(name = "categoryId",value = "类别编号",required = true),
@ApiImplicitParam(name = "categoryName",value = "类别名字",required = true),
@ApiImplicitParam(name = "categoryPicture1",value = "类别图片",required = false),
})//批量标记返回值
@PostMapping("/set")
public Category set(Long categoryId,String categoryName,String categoryPicture1){
Category category = new Category();
return category;
}
@GetMapping("/delete")
public Category delete(){
Category category = new Category();
return category;
}
}
3.访问生成的文档
- 项目启动后访问localhost:端口号/根路径/doc.html
4.API接口测试
- 在类别模块中选择需要测试的接口
5.调试选项
- 在调试选项中就可以像postman一样使用了