1 Star 4 Fork 2

潍坊雷鸣云网络科技有限公司 / javadoc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

一分钟放到项目中使用

1. pom.xml 中增加依赖

<!-- javadoc https://gitee.com/leimingyun/javadoc -->
<dependency>
  <groupId>com.xnx3.doc.javadoc</groupId>
  <artifactId>javadoc</artifactId>
  <version>1.16</version>
  <scope>compile</scope>
</dependency>

2. 一行代码导出

新建一个类,代码如下,直接运行,即可生成文档

package com;

/**
 * 自动扫描指定的包下所有controller的json接口,根据其标准的JAVADOC注释,生成接口文档。  
 * 使用参考 https://gitee.com/leimingyun/dashboard/wikis/leimingyun/wm/preview?sort_id=4518712&doc_id=1101390
 * @author 管雷鸣
 */
public class ApiDoc {
	public static void main(String[] args) {
		new com.xnx3.doc.JavaDoc("com.xnx3.demo").generateHtmlDoc();
	}
}

注释的写法

javadoc_example.png

Controller 类的注意事项

/**
 * 用户相关
 * @author 管雷鸣
 */
@Controller
@RequestMapping("/demo/simple/")
public class SimpleController{
	
}
  • 要有 @RequestMapping 注解,才会被扫描到

方法的注释及传入参数

/**
 * 传入姓名跟年龄,进行保存
 * @author 管雷鸣
 * @param name 姓名 
 * @param age 年龄
 * @param token 当前操作用户的唯一标识,登录标识 <required> <example=ef68481a-a467-44ae-8c16-952d6f5f009b>
 * @return 执行的结果
 */
@RequestMapping(value="setPerson.json", method = RequestMethod.POST)
@ResponseBody
public BaseVO setPerson(
		@RequestParam(required = false, defaultValue="管雷鸣") String name,
		@RequestParam(required = true, defaultValue="28") int age){	
	// ...
	return BaseVO.success("操作成功");
}
  • @ResponseBody、@RequestMapping(或 @PostMapping) 只有加了这两个注解,在生成文档时才会将此方法自动生成接口文档。
  • 传入的参数中 @RequestParam 的标注,其中
    • required 生成的文档说明中,这个传入参数是否必传项
    • defaultValue 生成的文档说明中,这个传入参数的示例值
  • 某个传如参数如果不需要有 @RequestParam ,那么不必为了应对文档而产生垃圾代码,可以直接在 javadoc 中的 @param 里使用指定几个标签进行标注
    • <required> 标注当前参数是否是必传。如果没有此标签,此传参默认为非必传
    • <type=int> 标注当前参数要传入的数据类型,如 <type=int><type=float> <type=string> 等,如果没有此标签,生成文档中的传入类型默认为 string 类型
    • <example=Java开发> 标注此传入参数可传入值的示例,在生成文档中的传入示例这里显示,让观看文档者能更好知晓要传入什么

方法(接口)返回响应

Controller 的方法:
/**
 * 获取某个人的信息
 * @author 管雷鸣
 * @return 某个人的信息
 */
@RequestMapping(value="gainPerson.json", method = RequestMethod.POST)
@ResponseBody
public PersonVO gainPerson(){	
	PersonVO vo = new PersonVO();
	
	// ... 省略
	
	return vo;
}
PersonVO:
package com.xnx3.demo.vo;
import com.xnx3.BaseVO;
import com.xnx3.demo.entity.Person;

/**
 * 人员信息(用于json接口的返回值响应)
 * @author 管雷鸣
 */
public class PersonVO extends BaseVO{
	private Person person;	//人员信息

	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
Person 实体类(或者 java bean 类):
package com.xnx3.demo.entity;

/**
 * 人员表,演示的实体类。这里演示就不加那些实体类相关注解了
 * @author 管雷鸣
 */
public class Person{
	private String name; 	//人员姓名
	private Integer age;	//年龄,几岁
	
	// get、set 、 tostring ... 这里只是演示就省略不写了
}

VO类以及实体类中注释采用直接在后面跟上 // 注释的方式。
注意,要在 private String xxx; //这后面跟上注释,不要放到上面。
这里的注释会在接口自动生成返回值时,将返回值的每个字段代表什么意思列出来

使用扩展

注释的美观程度

注释中可以使用html标签进行如换行等操作,美化输出的注释内容

多项目引用

如当前开发的项目,比如示例中的 BaseVO 是一个jar xnx3-util.jar (https://gitee.com/leimingyun/xnx3_util) 中的,在生成文档时 BaseVO 中的 result 、 info 是没有注释的,可以将xnx3-util项目也拉下来,比如你当前项目在 /mac/git/项目目录 ,那拉下来的 xnx3-util 也要在 /mac/git/ 目录下,这样生成文档时会自动扫描相关项目进行抽取注释。 这里面默认引入了 xnx3-util、 xnx3-weixin、wm、wangmarket、wangmarket_shop 等项目,如果你有其他jar包要引入,在生成文档的代码中可以这样增加

doc.javaSourceFolderList.add("/Users/apple/git/page.java/");

传入的这个 page.java 便是你项目拉下来后的目录名,注意这个是目录,最后要有 /
增加后完整的代码为

JavaDoc doc = new JavaDoc("com.xnx3.demo");
doc.javaSourceFolderList.add("/Users/apple/git/page.java/");
doc.generateHtmlDoc();

自定义文档模板

原始文档模板下载

....

模板中通用变量
  • {name} 当前文档的名字
  • {version} 当前文档的版本
  • {domain} 当前文档默认请求的接口域名,这里输出如 http://api.zvo.cn

以上三个变量在所有模板文件(index.html、template.html、style.css、javadoc.js)都可以直接使用。
其变量的内容,需要再生成文档时在Java代码中进行设置:

JavaDoc doc = new com.xnx3.doc.JavaDoc("com.xnx3.wangmarket.shop.api.controller");
doc.templatePath = "/Users/apple/Downloads/javadoc/";		//本地模板所在磁盘的路径
doc.name = "网市场云商城-用户端-API文档";				//文档的名字
doc.domain = "http://api.zvo.cn";				//文档中默认的接口请求域名
doc.version = "1.7";						//当前做的软件系统的版本号

doc.generateHtmlDoc();	//生成文档

常见问题

Eclipse中运行时,出现错误 java.lang.NoClassDefFoundError: com/sun/tools/javadoc/Main

常见于第一次在使用,eclipse用的默认自带的jre,运行时报错如下:

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/javadoc/Main
	at com.xnx3.doc.javadoc.JavaDocReader.readDoc(JavaDocReader.java:119)
	at com.xnx3.doc.javadoc.JavaDocUtil.getJavaDoc(JavaDocUtil.java:49)
	at com.xnx3.doc.JavaDoc.searchController(JavaDoc.java:227)
	at com.xnx3.doc.JavaDoc.generateHtmlDoc(JavaDoc.java:84)
	at cn.ApiDoc.main(ApiDoc.java:18)
Caused by: java.lang.ClassNotFoundException: com.sun.tools.javadoc.Main
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
	... 5 more
  1. 下载JDK8安装到电脑上
  2. 将jdk1.8.../lib/tools.jar 加入项目依赖,即可。

其他

项目简介

本项目最初为wm快速开发 (http://wm.zvo.cn) 中的文档自动生成模块,后独立出来,可用于任何 springmvc 的项目中进行使用,为接口生成接口文档。

完整代码DEMO示例

https://gitee.com/leimingyun/javadoc/tree/master/src/main/java/com/xnx3/demo

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

根据标准的 JavaDoc 注释生成接口文档,既能保证代码美观、注释完善、又能一键生成接口文档 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/leimingyun/javadoc.git
git@gitee.com:leimingyun/javadoc.git
leimingyun
javadoc
javadoc
master

搜索帮助