专业的JAVA编程教程与资源

网站首页 > java教程 正文

SpringCloud/SpringBoot - 自动生成API文档

temp10 2024-10-14 17:24:48 java教程 16 ℃ 0 评论

SpringCloud/SpringBoot 的项目一定要前后分离,这就需要一个API文档生成的工具,后端一定要和前端或者是移动端对接接口,那么问题来了,接口是不是要自己写给他们看,一般的会采用Excel或者Word来写,高级一点的就采用API管理平台手工录入,一个项目有上千上万个接口,天啊,这是多么大的工作量,在接口维护的时候更加痛苦,为了解决这样的事我们可以借助 japi 这个项目来完成RESTFul文档的自动生成,完全基于注释生成,更多详细配置可参考开源项目https://github.com/dounine/japi。

使用说明

SpringCloud/SpringBoot - 自动生成API文档

克隆项目下来

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已完全开源

最后

每天发布更多新鲜有含量的技术文章、总有一款适合你。

快关注我、把我打包带走。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表