SpringBoot如何自动生成API文档详解
作者:kikiki2 时间:2023-05-24 13:44:37
前言
在做项目的时候,如果项目是前后分离的,后端一定要和前端或者是移动端对接接口,那么问题来了,接口是不是要自己写给他们看,一般的会采用Excel或者Word来写,高级一点的就采用API管理平台手工录入,一个项目有上千上万个接口,天啊,这是多么大的工作量,在接口维护的时候更加痛苦,为了解决这样的事我们可以借助 japi 这个项目来完成RESTFul文档的自动生成,完全基于注释生成,更多详细配置可查看https://github.com/dounine/japi。
使用说明
克隆项目下来
git clone https://github.com/dounine/japi.git
编译打包
cd japi/java
gradle install -xtest
运行JAPI后台管理
cd japi/java
gradle bootRun
运行JAPI文档界面
cd japi/node
node app.js
生成RESTFul文档
maven项目
<dependency>
<groupId>com.dounine.japi</groupId>
<artifactId>client</artifactId>
<version>1.0</version>
</dependency>
gradle项目
compile group: 'com.dounine.japi', name: 'client', version: '1.0'
编辑resources/japi.properties配置文件
japi.name=test
japi.uuid=43a600877430438596de3d330e4bd06e
japi.version=1.0.0
japi.author=lake
japi.url=http://192.168.0.123:8080
japi.description=this is project description.
japi.createTime=2017-02-23 10:44:44
japi.icon=/home/lake/github/japi/html/img/logo.png
japi.server=http://192.168.0.179:7778
japi.server.username=japi
japi.server.password=japi123
编写一个action
/**
* 获取用户列表
* @param user 用户信息
* @return class User
* @version v2
*/
@GetMapping(value = "v2/list")
public Result hots(@Validated({User.UserDEL.class}) User user) throws RuntimeException {
return null;
}
编写一个API生成类
JapiCreateTest.java
@Test
public void testCreate(){
JapiClient.setPrefixPath("/home/lake/github/test-japi/java/");//项目路径前缀
JapiClient.setpostfixPath("/src/main/java");//项目路径后缀
JapiClient.setProjectJavaPath("client");//主项目地扯
JapiClient.setActionReletivePath("com/dounine/test-japi/action");//相对主项目action包所在路径
//JapiClient.setIncludeProjectJavaPath(new String[]{"api"});//主项目中关联的其它项目包路径
JapiClient.setIncludePackages(new String[]{"com.dounine.test-japi"});//关联的包,用于准确快速搜索
JapiClient.saveHistory(true);//是否保留本地历史版本
JapiClient.setFlushServer(false);//强制同步本地与服务器所有的版本(会先删除服务器以前的历史版本)
IProject project = ProjectImpl.init();
JapiClientStorage japiClientStorage = JapiClientStorage.getInstance();
japiClientStorage.setProject(project);
japiClientStorage.autoSaveToDisk();//自动使用到本地磁盘==> 用户目录/.japi-client/
new JapiClientTransfer().autoTransfer(japiClientStorage);//文件传输到主服务器.
}
最后访问浏览器 http://localhost:7777
就可看到RESTFul文档登录
使用自己的帐号登录就可以看到所有项目
进入项目看详细RESTFul接口
使用场景
JAPI最适合在SpringCloud这样的分布式多模块项目中使用,内置强大的正则表达式,这会让代码注释更加规范,区别于swigger这类使用侵入式注解生成文档的,JAPI是完全基于标准注释生成的,支持强大的JSR303或者是自定义注解。
开源精神
欢迎有意向的同学加入JAPI项目组将它改造得更好。
总结
来源:https://www.jianshu.com/p/8ac211d9c6d3
标签:springboot,接口,文档
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring源码解析 Bean属性填充
2021-06-28 19:17:16
![](https://img.aspxhome.com/file/2023/2/61612_0s.png)
C++优先队列用法案例详解
2022-10-23 17:35:01
java客户端Jedis操作Redis Sentinel 连接池的实现方法
2023-08-19 10:55:19
浅谈Java中Map和Set之间的关系(及Map.Entry)
2023-08-25 02:23:48
![](https://img.aspxhome.com/file/2023/9/57949_0s.jpg)
深入理解C#之枚举
2023-01-02 14:08:33
spring 整合mybatis后用不上session缓存的原因分析
2021-12-09 10:11:03
Bitmap海量数据快速查找去重代码示例
2021-12-01 12:21:07
C#实现的SQL备份与还原功能示例
2021-09-03 17:53:17
idea创建JAVA Class时自动生成头部文档注释的方法
2023-07-10 18:53:07
![](https://img.aspxhome.com/file/2023/5/69715_0s.png)
C#编程自学之开篇介绍
2023-12-16 07:58:55
![](https://img.aspxhome.com/file/2023/0/116440_0s.png)
zookeeper watch机制的理解
2021-11-08 11:36:36
一文精通Java中的volatile关键字
2023-11-24 04:17:14
![](https://img.aspxhome.com/file/2023/2/59572_0s.png)
Android 自定义底部上拉控件的实现方法
2023-08-18 18:41:19
![](https://img.aspxhome.com/file/2023/3/137423_0s.gif)
Java for循环标签跳转到指定位置的示例详解
2022-10-04 17:52:31
深入解析Android App的LayoutInflate布局
2022-01-28 19:05:26
![](https://img.aspxhome.com/file/2023/3/139423_0s.jpg)
详解C#之委托
2021-12-12 00:17:49
![](https://img.aspxhome.com/file/2023/0/79100_0s.png)
Java零基础精通方法篇
2021-08-29 12:07:58
![](https://img.aspxhome.com/file/2023/1/75481_0s.png)
java 文件下载支持中文名称的实例
2023-03-16 09:02:16
JavaFx实现拼图游戏
2022-04-20 01:05:05
![](https://img.aspxhome.com/file/2023/4/69714_0s.jpg)
spring cloud将spring boot服务注册到Eureka Server上的方法
2023-12-08 19:42:09
![](https://img.aspxhome.com/file/2023/7/60837_0s.png)