【JAVA】javadoc,如何生成标准的JAVA API文档

简介: 【JAVA】javadoc,如何生成标准的JAVA API文档

1.什么是JAVA DOC

当我们写完JAVA代码,别人要调用我们的代码的时候要是没有API文档是很痛苦的,只能跟进源码去一个个的看,一个个方法的猜,并且JAVA本来就不是一个重复造轮子的游戏,一般一些常用的轮子早就已经早好了,直接拿来用就是。但是拿来用的时候往往由于API文档的缺失或者不规范,造成使用上的很多痛苦,大家在很多实际工作中经常也会遇到类似的场景:


公司多年累积下来的工具类或者提供底层能力的公共模块里面积累了很多能力,公司为了代码规范也要求我们尽量去调用这些工具类或者公共模块。但是:


没有API文档或者文档写的很烂


参数列表动不动就很长,数十个甚至几十个参数


参数列表没有注释,出现一些莫名其妙的参数,都不知道怎么传


方法名也不能见名知意


造成往往要用这些公共能力的时候甚至还要去读源码


有读源码这个时间,可能自己都重新写了一个了,而且自己写的,可能比祖传下来的那些工具类还更“清爽”一些。于是系统内越来越多工具类堆积,重复造的轮子越来越多,“屎山”越堆越高。


即使有时候我们有API文档,但是各类API文档,格式,内容都不相同,没有统一的规范,读起来其实也很慢。所以有没有一个统一的规范喃?JAVA官方其实早就想到了这个问题,在JDK1.1发布的时候就附带了JAVA DOC,支持用标签注释的方式给各个方法做好规范的说明,然后用JAVA命令一键生成可视化的HTML页面作为API。

2.标签

标签是JAVA DOC的核心,用什么标签,JAVA DOC最后就会对应生成哪些API文档内容:

@author:

    /**
    * @author John Doe
    */
   public class MyClass {
   }
   @version:

   /**
    * @version 1.0.1
    */
   public class MyClass {
   }
@param:

   /**
    * Concatenates two strings.
    * @param string1 The first string to concatenate.
    * @param string2 The second string to concatenate.
    * @return The concatenated string.
    */
   public String concatenateStrings(String string1, String string2) {
       return string1 + string2;
   }
@return:

   /**
    * Returns the sum of two integers.
    * @param num1 The first integer.
    * @param num2 The second integer.
    * @return The sum of num1 and num2.
    */
   public int add(int num1, int num2) {
       return num1 + num2;
   }
@throws 或 @exception: 描述方法可能抛出的异常。

   /**
    * Divides two numbers and throws an exception if the divisor is zero.
    * @param dividend The number to be divided.
    * @param divisor The divisor.
    * @return The result of the division.
    * @throws ArithmeticException If the divisor is zero.
    */
   public double safeDivide(double dividend, double divisor) {
       if (divisor == 0) {
           throw new ArithmeticException("Divisor cannot be zero.");
       }
       return dividend / divisor;
   }
@see:

   /**
    * See {@link java.util.ArrayList} for more information on dynamic arrays.
    */
   public class MyDynamicArray {
   }
@link: 创建一个链接到其他类、方法或字段的链接。

   /**
    * This method uses the {@link java.util.Collections#shuffle(List)} method to randomize the list.
    */
   public void shuffleList(List<?> list) {
       Collections.shuffle(list);
   }
@since: 指定该元素是从哪个版本开始引入的。

   /**
    * A utility class for working with dates.
    * @since 1.5
    */
   public class DateUtils {
   }
@deprecated: 标记不再推荐使用的元素。

   /**
    * Old method that should not be used anymore.
    * @deprecated Use the {@link #newMethod()} instead.
    */
   @Deprecated
   public void oldMethod() {
   }
@inheritDoc: 继承父类或接口的 JavaDoc。

    /**
     * {@inheritDoc}
     */
    @Override
    public void someMethod() {
        // Implementation
    }
@parametricType: 用于描述泛型类型参数。

    /**
     * Represents a generic collection.
     * @param <E> The type of elements in this collection.
     */
    public class MyCollection<E> {
    }
@serialField 和 @serialData: 用于序列化类的字段和数据。

/**
 * A serializable class.
 * @serialField name The name of the object.
 * @serialData The length of the name.
 */
@Serial
private static final long serialVersionUID = 1L;
private String name;
// serialization logic
 

3.命令

javadoc命令用于生成API文档,其支持多种参数:

javadoc [options] [source files]

常用参数:

-d <directory>: 指定输出目录,存放生成的 HTML 文件。

-sourcepath <pathlist>: 指定源文件路径,可以是多个路径,用分隔符(如冒号或分号)分隔。

-subpackages <packagename>: 递归处理指定包及其子包下的所有类。

-classpath <classpath>: 设置类路径,用于解析类型引用。

-encoding <encoding>: 指定源文件的字符编码。

-charset <charset>: 指定生成文档时使用的字符集。

-windowtitle <text>: 设置文档窗口标题。

-doctitle <text>: 设置文档页面的标题。

-overview <filename>: 指定概述文件,用于文档的首页内容。

-exclude <patternlist>: 指定要排除的包或类的模式列表。

-private: 包含私有成员的文档。

-protected: 默认行为,包含受保护和公开成员的文档。

-public: 只包含公共成员的文档。

示例:


假设你有一个名为 com.example 的包,位于 /src/main/java 目录下,你想生成包含所有公共和受保护成员的 API 文档,并将输出文件保存在 /docs/api 目录下,同时指定字符编码为 UTF-8,可以使用以下命令:


javadoc -encoding UTF-8 -charset UTF-8 -d /docs/api -sourcepath /src/main/java -subpackages com.example


搞一个类来把注解都用上,然后生成API文档看看:

package com.eryi.config;
 
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
 
/**
 * @author John Doe
 * @version 1.0.0
 * @since 2022-04-01
 * @deprecated Since version 1.1.0, use the {@link BetterFileManager} instead.
 * This class provides basic file management operations.
 */
@Deprecated
public class FileManager {
 
    /**
     * @param filePath The path of the file to check.
     * @return True if the file exists, false otherwise.
     * @see File#exists()
     * @throws NullPointerException If the filePath is null.
     */
    public boolean fileExists(String filePath) {
        if (filePath == null) {
            throw new NullPointerException("FilePath cannot be null.");
        }
 
        File file = new File(filePath);
        return file.exists();
    }
 
    /**
     * @param fileName The name of the file to create.
     * @param content The content to write into the file.
     * @return The newly created file.
     * @throws IOException If there's any issue creating or writing to the file.
     * @see File#createNewFile()
     * @see FileWriter
     */
    public File createFileWithContent(String fileName, String content) throws IOException {
        File file = new File(fileName);
        if (!file.createNewFile()) {
            throw new IOException("Failed to create file: " + fileName);
        }
 
        try (FileWriter writer = new FileWriter(file)) {
            writer.write(content);
        }
        return file;
    }
 
    /**
     * A sample file path constant.
     * @since 1.0.0
     */
    @Deprecated
    public static final String SAMPLE_FILE_PATH = "resources/sample.txt";
 
    /**
     * @param args Command-line arguments (not used in this example).
     */
    public static void main(String[] args) {
        FileManager fileManager = new FileManager();
        System.out.println("Does sample file exist? " + fileManager.fileExists(SAMPLE_FILE_PATH));
    }
}

直接JAVADOC:

会在路径下生成一堆东西,需要用的只有index.html,其它的都是为了支持这个index.html的资源文件而已:

看看效果:

可以看到关于这个类的什么都有:

目录
相关文章
|
4天前
|
Java API
深入探讨 Java 8 集合操作:全面解析 Stream API 的强大功能
深入探讨 Java 8 集合操作:全面解析 Stream API 的强大功能
14 2
|
4天前
|
SQL Java API
Java一分钟之-JPA查询:JPQL与Criteria API
【6月更文挑战第14天】本文探讨了Java Persistence API (JPA)中的两种查询方式:JPQL和Criteria API。JPQL是面向对象的SQL,适用于简单查询,而Criteria API则提供类型安全的动态查询构造。文章指出了每种方法的常见问题和避免策略,如混淆实体属性与数据库字段、参数绑定错误、过度复杂化和性能问题。建议开发者根据需求选择适当的方法,并关注查询的可读性、可维护性和性能优化。
19 2
|
1天前
|
SQL Java API
【Java】Java流中的API
【Java】Java流中的API
8 5
|
5天前
|
分布式计算 自然语言处理 大数据
【大数据】MapReduce JAVA API编程实践及适用场景介绍
【大数据】MapReduce JAVA API编程实践及适用场景介绍
16 0
|
5天前
|
Java 大数据 API
【大数据】HDFS、HBase操作教程(含指令和JAVA API)
【大数据】HDFS、HBase操作教程(含指令和JAVA API)
35 0
【大数据】HDFS、HBase操作教程(含指令和JAVA API)
|
4天前
|
安全 数据挖掘 API
【实时数据】商品详情API接口系列开发
商品详情API接口系列对于电商平台至关重要,它们为开发者提供了实时、准确的商品信息,使得在线销售和展示商品变得更加高效和精准。以下是几个主要的电商平台及其商品详情API接口的介绍:
|
13天前
|
监控 供应链 搜索推荐
数据驱动电商:深度利用淘宝API接口掌握商品详情
本文探讨了如何利用淘宝API接口获取商品详情数据以助力电商决策。通过API,商家能获取商品标题、价格、库存等信息,从而进行市场分析、库存优化、定价策略制定及个性化推荐。步骤包括注册获取API权限、理解文档、构建数据收集流程、处理分析数据以及应用结果。示例代码展示了如何用Python调用API获取商品详情。善用API和数据驱动策略可在电商市场中取得优势。请注意遵循淘宝的API使用规范。
|
4天前
|
人工智能 自然语言处理 API
云栖发布:通义听悟AI能力再进化,开放API接口
云栖发布:通义听悟AI能力再进化,开放API接口
|
5天前
|
JSON 安全 API
电商开发者必读:微店商品详情API接口全解析
微店商品详情API让开发者能通过商品ID获取包括名称、价格、库存、描述和图片在内的详细信息。开发者需注册账号、获取API密钥和访问权限,并熟悉HTTP请求。请求示例为GET方法,响应数据以JSON格式返回。注意错误处理、保密性、频率限制和数据验证,以确保安全和高效使用。
|
5天前
|
搜索推荐 Java API
探索淘宝API接口:为电商业务开启无限可能
淘宝API接口是淘宝平台为第三方开发者提供的一组开放接口,通过这些接口,开发者可以获取淘宝平台上的商品信息、交易数据、用户行为等关键数据,并基于这些数据开发各种应用。淘宝API接口支持多种编程语言,如Java、Python、PHP等,方便开发者根据自己的需求进行选择和开发。