自动生成服务接口文档

生成格式如下

自动生成服务接口文档_第1张图片
Paste_Image.png
  1. 加入javadoc插入
    
        
            
                maven-javadoc-plugin
                true
                
                    
                        compile
                        
                            aggregate
                        
                    
                
                
                    com.glsx.platform.protocol.common.ExtractCommentsDoclet
                    ${project.build.directory}/classes
                    ${project.build.outputDirectory}/META-INF
                    false
                
            
        
    
    
        
            default-tools.jar
            
                
                    java.vendor
                    Oracle Corporation
                
            
            
                
                    com.sun
                    tools
                    1.4.2
                    system
                    ${java.home}/../lib/tools.jar
                
            
        
    
  1. 代码

import com.sun.javadoc.*;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;

/**
 * Created by linxy on 2017/2/9.
 */
public class ExtractCommentsDoclet {

    public static boolean start(RootDoc root) throws IOException {
        generateApiDoc(root, "api.docx", "com.xxx.service");
        return true;
    }


    public static void generateApiDoc(RootDoc root, String filename, String packagePath) throws IOException {

        XWPFDocument document= new XWPFDocument();
        FileOutputStream out = new FileOutputStream(new File(filename));

        XWPFParagraph p = null;
        XWPFRun r = null;


        PackageDoc packageDoc = root.packageNamed(packagePath);

        for (ClassDoc c : packageDoc.allClasses()) {

            for (MethodDoc m : c.methods(false)) {

                p = document.createParagraph();

                p.setAlignment(ParagraphAlignment.LEFT);
                r = p.createRun();
                r.setBold(true);
                r.setFontSize(15);
                r.setText("接口名称");

                p = document.createParagraph();
                p.setIndentationFirstLine(400);
                r = p.createRun();
                r.setText(m.name());


                p = document.createParagraph();
                p.setAlignment(ParagraphAlignment.LEFT);
                r = p.createRun();
                r.setBold(true);
                r.setFontSize(15);
                r.setText("接口描述");

                p = document.createParagraph();
                p.setIndentationFirstLine(400);
                r = p.createRun();
                r.setText(m.commentText());

                p = document.createParagraph();
                p.setAlignment(ParagraphAlignment.LEFT);
                r = p.createRun();
                r.setBold(true);
                r.setFontSize(15);
                r.setText("输入参数");


                Parameter[] parameters = m.parameters();

                if(parameters.length > 0) {
                    int totalRow = parameters.length+1;
                    int totalCol = 4;

                    String[][] content = new String[totalRow][totalCol];
                    content[0][0] = "参数名称";
                    content[0][1] = "类型";
                    content[0][2] = "是否必填";
                    content[0][3] = "描述信息";

                    for (int i = 0; i < totalRow-1; i++) {
                        Parameter param = parameters[i];

                        content[i+1][0] = param.name();
                        content[i+1][1] = param.typeName();
                        content[i+1][2] = "Y";
                        content[i+1][3] = "";

                        for (ParamTag paramTag : m.paramTags()) {
                            if (param.name().equals(paramTag.parameterName())) {
                                content[i+1][3] = paramTag.parameterComment();
                            }
                        }
                    }

                    XWPFTable table = document.createTable(totalRow, totalCol);

                    for(int i = 0; i < totalRow; i++) {
                        XWPFTableRow row = table.getRow(i);
                        CTTrPr trPr = row.getCtRow().addNewTrPr();
                        CTHeight ht = trPr.addNewTrHeight();
                        ht.setVal(BigInteger.valueOf(360L));
                        List cells = row.getTableCells();

                        for(int j = 0; j < totalCol; ++j) {
                            XWPFTableCell cell = row.getCell(j);

                            CTTcPr tcpr = cell.getCTTc().addNewTcPr();
                            CTVerticalJc va = tcpr.addNewVAlign();
                            va.setVal(STVerticalJc.CENTER);

                            CTShd ctshd = tcpr.addNewShd();
                            ctshd.setColor("auto");
                            ctshd.setVal(STShd.CLEAR);

                            if(i == 0) {
                                //设置表头颜色
                                ctshd.setFill("A7BFDE");
                            }

                            // 水平居中
                            cell.getParagraphs().get(0).setAlignment(ParagraphAlignment.CENTER);
                            cell.setText(content[i][j]);
                        }
                    }
                }
            }
        }

        document.write(out);
        out.close();
    }


}

你可能感兴趣的:(自动生成服务接口文档)