跟大家介绍一个自动生成接口文档的工具包,作者的理念是注释即文档,在写代码的时候写上注释,项目启动后就会生成接口文档,非常方便,省去了Swagger写注解的过程。
仓库地址:https://github.com/YeDaxia/JApiDocs
中文文档:https://japidocs.agilestudio.cn/#/zh-cn/
1.依赖
io.github.yedaxia
japidocs
1.4.3
2.启动类添加生成代码
public static void main(String[] args) {
DocsConfig config = new DocsConfig();
config.setProjectPath("D:\\Project\\japidocs"); // 项目根目录
config.setProjectName("ProjectName"); // 项目名称
config.setApiVersion("V1.0"); // 声明该API的版本
config.setDocsPath("D:\\Project\\japidocs\\docs"); // 生成API 文档所在目录
config.setAutoGenerate(Boolean.TRUE); // 配置自动生成
Docs.buildHtmlDocs(config); // 执行生成文档
SpringApplication.run(JapidocsApplication.class, args);
}
3.项目启动后控制台会出现这样的信息,然后在docs文件看到生成的文档
class Test1 {
/**
* 参数1
*/
private int a;
private String b; // 参数2
private Date c;
}
@PostMapping("/test-3-post1")
public Test1 test3post(@RequestBody Test1 test ) {
return test;
}
生成的文档
/**
* 这个接口是干嘛的
* @description 这是一行说明 哈哈哈~~~~
* @param test 这是个对象 true
* @author lkz
* @return 一个类
*/
@PostMapping("/test-3-post1")
public Test1 test3post(@RequestBody Test1 test ) {
return test;
}
生成文档
/**
*
* @param path
* @param a
* @return
*/
@GetMapping("/test-5-get213/{path}")
public String test5ge2t3(@PathVariable(value = "path", required = false) Integer path,
@RequestParam(value = "a", required = false) String a) {
return String.valueOf(path) + a;
}
生成文档
/**
*
* @param path
* @param a
* @return 一个String
*/
@GetMapping("/test-5-get213/{path}")
public String test5ge2t3(@PathVariable(value = "path", required = true) Integer path,
@RequestParam(value = "a", required = true) String a) {
return String.valueOf(path) + a;
}
生成文档
这里的显示支持html标签
/**
* 这个接口是干嘛的 POST
* @description 这是一行说明 哈哈哈~~~~
* @param test 这是个对象 true
* @author lkz
* @return 一个类
*/
@PostMapping("/test-3-post1")
public Test1 test3post(@RequestBody Test1 test ) {
return test;
}
/**
* get请求 GET
* @param path
* @param a
* @return 一个String
*/
@GetMapping("/test-5-get213/{path}")
public String test5ge2t3(@PathVariable(value = "path", required = true) Integer path,
@RequestParam(value = "a", required = true) String a) {
return String.valueOf(path) + a;
}
生成文档
JApiDocs用法基本就这样了,非常简单,美中不足的是不支持像swagger那样在线调用,大家去作者GitHub催催他添加这个功能把。