1.knife4j快速入门:
knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案。说白了就是swagger文档的增强版,
特点:修改了展示方式,swagger为上下展示;knife4j为左右展示《更符合中国人的阅读习惯》,测试功能更加详细与简单明了
2.springboot开始整合knife4j文档:
添加pox.xm依赖;
<!-- knife4j依赖 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
编写Knife4jConfiguration配置类;
@Configuration
@EnableSwagger2
public class Knife4jConfiguration {
@Bean(value = "defaultApi2")
/**
* 根据自己的需求修改信息
*/
public Docket defaultApi2() {
Docket docket=new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.title("测试==demo")
.description("# swagger-bootstrap-ui-demo RESTful APIs")
.termsOfServiceUrl("http://www.xx.com/")
.contact(new Contact("玉面小白龙","","123@qq.com"))
.version("1.0")
.build())
//分组名称
.groupName("2.X版本")
.select()
//这里指定文档Controller层扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.xxx.xxx.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
}
最后注解的实现
controller层: 使用的位置
@Api(tags = "学生模块")
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@Autowired
private StudentMapper studentMapper;
/**
* 根据学号查询数据
* @param studentid
* @return
*/
@ApiOperationSupport(author = "杨涛涛")
@ApiOperation(value ="根据学号查询")
@ApiImplicitParam(name = "studentid",value = "学生学号",readOnly = true,dataTypeClass = Integer.class)
@GetMapping("getById/{studentid}")
public R getById(@PathVariable("studentid") Integer studentid){
Student byId = studentService.getById(studentid);
return R.ok().data("byId",byId);
}
}
访问路径 :http://localhost:8080/doc.html
更加直观的测试方式:
注意事项:
使用knife4j文档切记方法上不要是用@RequestMapping注解来交互数据;不然展示的效果入图:一个方法会帮你全部的增删查改方法全部展现出来;实际开发中 一个接口只能对应一个测试方法,查询就用@GetMapping 增加就用@PostMapping 修改就用@PutMapping 删除就用@deleteMapping