Search in sources :

Example 1 with DocOAuth20SubIn

use of info.xiancloud.core.apidoc.annotation.DocOAuth20SubIn in project xian by happyyangyuan.

the class OAuth20MdBuilderHandler method build.

@Override
public void build() {
    LOG.info("-----Oauth20接口文档构建开始-----");
    LOG.info("-----Auth20接口开始扫描-----");
    List<Class> list = Reflection.getWithAnnotatedClass(DocOAuth20.class, "com.apifest");
    if (list == null || list.isEmpty()) {
        LOG.info("----Auth20接口扫描完成,暂无相关信息,构建退出---");
        invokeCallback(null);
        return;
    }
    System.out.println(String.format("----Auth20接口扫描完成%s---", list.size()));
    try {
        LOG.info("----Auth20接口文档开始生成---");
        // FileWriter fw = new FileWriter(storePath);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Writer wout = new OutputStreamWriter(bos);
        BufferedWriter bw = new BufferedWriter(wout);
        bw.write("# OAuth20接口文档\r\n");
        for (Class oauthApi : list) {
            // 获取所有方法
            Method[] methods = oauthApi.getDeclaredMethods();
            for (Method method : methods) {
                DocOAuth20Sub sub = method.getAnnotation(DocOAuth20Sub.class);
                // 该方法属于接口
                if (sub != null) {
                    String name = sub.name();
                    String dec = sub.dec();
                    String url = sub.url();
                    String httpMethod = sub.method();
                    bw.write(String.format("## 接口 %s", name));
                    bw.newLine();
                    bw.write(String.format(" * 接口描述%s\r\n", dec));
                    bw.write(String.format(" * 接口路径%s\r\n", url));
                    bw.write(String.format(" * 请求方式%s\r\n", httpMethod));
                    // System.out.println(String.format("name[%s],dec[%s],url[%s],httpMethod[%s]",
                    // sub.name(),
                    // sub.dec(), sub.url(), sub.method()));
                    // 接口入参
                    DocOAuth20SubIn[] args = sub.args();
                    bw.write(" * 入参数据结构说明\r\n");
                    bw.newLine();
                    bw.write(" <table class='table table-bordered table-striped table-condensed'>");
                    bw.newLine();
                    bw.write("<tr><td>名称</td><td>数据类型</td><td>参数说明</td><td>必须</td></tr>");
                    bw.newLine();
                    if (args != null && args.length > 0) {
                        for (DocOAuth20SubIn arg : args) {
                            bw.write("<tr>");
                            bw.newLine();
                            bw.write(String.format("<td>%s</td><td>%s</td><td>%s</td><td>%s</td>", arg.name(), arg.type(), arg.dec(), arg.require() ? "是" : "否"));
                            bw.newLine();
                            bw.write("</tr>");
                        }
                    }
                    bw.write("</table>\r\n");
                    bw.newLine();
                }
            }
        }
        bw.flush();
        bw.close();
        LOG.info("-----Oauth20接口文档构建结束-----");
        invokeCallback(bos.toByteArray());
    } catch (Exception e) {
        LOG.error(e);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Method(java.lang.reflect.Method) BufferedWriter(java.io.BufferedWriter) DocOAuth20Sub(info.xiancloud.core.apidoc.annotation.DocOAuth20Sub) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) DocOAuth20SubIn(info.xiancloud.core.apidoc.annotation.DocOAuth20SubIn)

Aggregations

DocOAuth20Sub (info.xiancloud.core.apidoc.annotation.DocOAuth20Sub)1 DocOAuth20SubIn (info.xiancloud.core.apidoc.annotation.DocOAuth20SubIn)1 BufferedWriter (java.io.BufferedWriter)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 Method (java.lang.reflect.Method)1