knife4j集合化postman
01 knife4j的介绍
- 基于 JavaMVC的集成框架swagger的进一步强化,在原有通过注释就能生成文档的前身swagger-bootstrap-ui之上,增加了postman的测试功能,优化了文档的UI界面,在测试api接口的方面有了极大的进步
02 前期准备
1.引入依赖
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>2.0.9</version>
</dependency>
2.配置yml文件
server:port: 8080servlet:context-path: /web
spring:mvc:pathmatch:matching-strategy: ant_path_matcher
3.引入配置类
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {@Beanpublic 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.测试的实体类
@ApiModel("类别实体类")//用于标记实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Category {@ApiModelProperty("类别编码")//用于标记实体类属性作用private Long categoryId;private String categoryName;private String categoryPicture1;private String categoryPicture2;
}
2.测试的控制类
@Api(tags = "类别模块")
@RestController
@RequestMapping("/category")
public class CategoryController {@ApiOperation(value = "查询类别",notes = "根据类别id查询类别" )@GetMapping("/select")public Category select(Long categoryId){Category category = new Category();return category;}@ApiImplicitParam(name = "categoryId",value = "类别编号",required = true)@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.调试选项
