整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

如何制作API文档

编今天学习java,学习了一种制作自己的API文档的方法,制作的方法应该有好多,小编先把自己知道的这一种分享给大家。

我是在学习java的时候学到的,是在eclipse上制作的,下面就来一步一步的制作。

1. 先建一个java工程

2.双击工程-->右键src--> new --> Package(建立一个包)

3.在这个包内建立一个类(右键test(包)--> new -->class)

4.然后就写代码,注意用(/** */)写有关方法的注释

5.准备工作就做完了,我们右键工程(Demo)-->Export...

这里是选择你下载的jdk的安装目录中的bin的javadoc

6.选择输出的文件目录就可以了

7.最后在文件目录中点indec.html就是自己做的API文档了


8.最后就是这样了

小编也是刚学会的,大神们可能有更好的方法,这里只是分享一下,大神如果有跟好的方法,可以分享出来哦。

、接口文档面对的困境

我工作几年,接口文档用过好几种方式了。从最开始的word文档,到后来的swagger和confluence编写接口文档,再到后来侵入性很小的jApiDoc,最后到现在的smart-doc工具。

对比下他们的优缺点:

方式

好处

缺点

word文档和confluence

有文档留存(好像也不算好处)

费时费力、多人编写不便

swagger

1、不用专门写文档

2、通过连接直接访问

3、在线测试,有点像简化的postman

注释太多,写得想打人

jApiDoc

1、引入jar包,一键生成html接口文档

2、侵入小,添加简单注释就行

1、功能单一,只能接口文档

2、作者好久没有维护了

smart-doc

1、引入maven插件,一键生成HTML接口文档

2、作者很活跃,社区也很活跃,反应问题很快就有新版本解决

3、能生成常用的html,markdown、postman接口文档

4、侵入小,添加简单注释就行

5、适配单服务、微服务等多种环境

1、需要抽两个小时看下官方文档

2、JApiDocs简介

前面我介绍过一种工具,叫做JApiDocs,这个工具我也使用了一段时间,用起来还是不错的,能满足基本要求,文档链接地址

3、前言

被写接口文档难受了好久,使用swagger要加各种稀奇古怪的注释,十分繁琐,突然看到JApiDocs 的介绍,只需要在接口上加上点注释,就能够生成接口文档。突然来了希望,通过看文档自己使用之后,把踩过的坑记录下来

生成的接口文档页面展示:

查询接口

新增接口

phinx简介

sphinx是一种基于Python的文档工具,它可以令人轻松的撰写出清晰且优美的文档,由Georg Brandl在BSD许可证下开发。新版的Python3文档就是由sphinx生成的,并且它已成为Python项目首选的文档工具,同时它对C/C++项目也有很好的支持。更多详细特性请参考spinx官方文档,本篇博客主要介绍如何快速为你的Python注释生成API文档。

sphinx官方文档链接:https://zh-sphinx-doc.readthedocs.io/en/latest/intro.html

环境

sphinx需要依赖Python环境,所以在使用sphinx之前需要先安装Python

  • 需要安装python
  • 安装sphinx
pip install sphinx

实例

新建一个项目

目录结构如上图所示,doc目录使用来存放API文档,src目录是用来存放项目的源码。

src目录下的源码

#coding=UTF-8
class Demo1():
 """类的功能说明"""
 def add(self,a,b):
 """两个数字相加,并返回结果"""
 return a+b
 def google_style(arg1, arg2):
 """函数功能.
 函数功能说明.
 Args:
 arg1 (int): arg1的参数说明
 arg2 (str): arg2的参数说明
 Returns:
 bool: 返回值说明
 """
 return True
 def numpy_style(arg1, arg2):
 """函数功能.
 函数功能说明.
 Parameters
 ----------
 arg1 : int
 arg1的参数说明
 arg2 : str
 arg2的参数说明
 Returns
 -------
 bool
 返回值说明
 """
 return True

demo1文件,主要使用了两种不同的Python注释分格。对于简单的例子和简单的函数以及文档说明,使用google style显得更为简洁,而对于比较复杂详细的文档说明numpy style更为流行。

#coding=UTF-8
def my_function(a, b):
 """函数功能说明
 >>> my_function(2, 3)
 6
 >>> my_function('a', 3)
 'aaa'
 """
 return a * b

demo2文件的注释看起来像Python命令行输入的文档字符串,主要是用来检查命令输出是否匹配下行的内容,它允许开发人员在源码中嵌入真实的示例和函数的用法,还能确保代码被测试和工作。

使用sphinx建立API文档项目

  • 进入到doc目录下
cd 项目路径/doc

输入sphinx-quickstart命令,会输出选项

> Root path for the documentation [.]: sphinx_demo
> Separate source and build directories (y/n) [n]: y
> Name prefix for templates and static dir [_]:
> Project name: sphinx_demo
> Author name(s): sphinx demo
> Project version []: 1.0
> Project release [1.0]:
> Project language [en]: zh_CN
> Source file suffix [.rst]:
> Name of your master document (without suffix) [index]:
> Do you want to use the epub builder (y/n) [n]:
> autodoc: automatically insert docstrings from modules (y/n) [n]: y
> doctest: automatically test code snippets in doctest blocks (y/n) [n]: y
> intersphinx: link between Sphinx documentation of different projects (y/n) [n]: y
> todo: write "todo" entries that can be shown or hidden on build (y/n) [n]: y
> coverage: checks for documentation coverage (y/n) [n]: y
> imgmath: include math, rendered as PNG or SVG images (y/n) [n]: y
> mathjax: include math, rendered in the browser by MathJax (y/n) [n]: y
> ifconfig: conditional inclusion of content based on config values (y/n) [n]:
> viewcode: include links to the source code of documented Python objects (y/n) [n]:
> githubpages: create .nojekyll file to publish the document on GitHub pages (y/n) [n]:
> Create Makefile? (y/n) [y]:
> Create Windows command file? (y/n) [y]:

因为我们需要从Python代码的注释中自动导出API文档,所以需要将autodoc: automatically insert docstrings from modules (y/n) [n]: y如果忘记设置,可以在conf.py中的extensions中添加'sphinx.ext.autodoc'。选项后面没有输入的,直接按回车键使用默认设置。选项后面有输入的,按照我的设置即可,如果不使用中文文档,可以在language配置中使用默认设置。设置完成之后,可以看到如下的目录结构

后面如果需要修改配置,选项在source/conf.py文件中修改即可。

extensions = ['sphinx.ext.autodoc',
 'sphinx.ext.doctest',
 'sphinx.ext.intersphinx',
 'sphinx.ext.todo',
 'sphinx.ext.coverage',
 'sphinx.ext.mathjax']

通过设置conf.py中的extensions,可以为sphinx添加额外的扩展,如果想要将html文档转换为PDF,只需要先安装扩展,然后再此处添加即可使用。由于我们的注释代码主要同时支持google style和numpy style,所以我们需要添加一个扩展来支持。

sphinx.ext.napoleon

为源码生成html文件

  • 修改source/conf.py文件的19-21行
import os
import sys
sys.path.insert(0, os.path.abspath('../../../src'))#指向src目录

将命令行切换到doc目录下,执行以下命令

sphinx-apidoc -o sphinx_demo/source ../src/
>Creating file sphinx_demo/source\demo1.rst.
>Creating file sphinx_demo/source\demo2.rst.
>Creating file sphinx_demo/source\modules.rst.

清理文件

cd sphinx_demo
make clean
>Removing everything under 'build'...

生成html文件

make html

请确保这一步没有输出error和exception

打开build/html/index.html

修改API的主题

打开source/conf.py文件,找到html_theme = 'alabaster',修改即可,sphinx官方提供了几种主题可以进行选择,sphinx主题设置

主题设置链接:http://www.sphinx-doc.org/en/master/theming.html

相关错误解决办法

SyntaxError:Non-ASCII character '\xba' in file .....py

在*.py文件的第一行添加#coding=UTF-8

Encoding error:'utf8' codec can't decode byte 0xc0 in position 44:invalid start byte

确保*.py文件的编码格式为utf-8,通过notepad++可以进行查看,如果不是请修改为utf-8格式

添加sphinx.ext.napoleon后报Exception occurred ....return translator['sphinx'].ugettext(message) KeyError:'sphinx'

Sphinx1.3,napoleon扩展使用sphinx.ext.napoleon,Sphinx <= 1.2使用sphinxcontrib.napoleon