diff --git a/src/org/redkale/boot/RestDocs.java b/src/org/redkale/boot/RestDocs.java index b39b61123..9f1e24258 100644 --- a/src/org/redkale/boot/RestDocs.java +++ b/src/org/redkale/boot/RestDocs.java @@ -6,10 +6,11 @@ package org.redkale.boot; import java.io.*; -import java.lang.reflect.Method; +import java.lang.reflect.*; import java.util.*; import org.redkale.convert.json.JsonConvert; import org.redkale.net.http.*; +import org.redkale.util.Comment; /** * 继承 HttpBaseServlet 是为了获取 WebAction 信息 @@ -26,6 +27,8 @@ public class RestDocs extends HttpBaseServlet { public void run() throws Exception { List serverList = new ArrayList<>(); + + Map>> typesmap = new LinkedHashMap<>(); for (NodeServer node : app.servers) { if (!(node instanceof NodeHttpServer)) continue; final Map map = new LinkedHashMap<>(); @@ -35,7 +38,7 @@ public class RestDocs extends HttpBaseServlet { List servletsList = new ArrayList<>(); map.put("servlets", servletsList); for (HttpServlet servlet : server.getPrepareServlet().getServlets()) { - if (!(servlet instanceof RestHttpServlet)) continue; + if (!(servlet instanceof HttpServlet)) continue; WebServlet ws = servlet.getClass().getAnnotation(WebServlet.class); if (ws == null) { System.err.println(servlet + " not found @WebServlet"); @@ -69,19 +72,51 @@ public class RestDocs extends HttpBaseServlet { actionmap.put("params", paramsList); for (WebParam param : action.params()) { final Map parammap = new LinkedHashMap<>(); + final boolean isarray = param.type().isArray(); + final Class ptype = isarray ? param.type().getComponentType() : param.type(); parammap.put("name", param.value()); parammap.put("radix", param.radix()); - parammap.put("type", param.type().getName()); + parammap.put("type", ptype.getName() + (isarray ? "[]" : "")); parammap.put("src", param.src()); + parammap.put("comment", param.comment()); paramsList.add(parammap); + if (ptype.isPrimitive() || ptype == String.class) continue; + if (typesmap.containsKey(ptype.getName())) continue; + + final Map> typemap = new LinkedHashMap<>(); + Class loop = ptype; + do { + if (loop == null || loop.isInterface()) break; + for (Field field : loop.getDeclaredFields()) { + if (Modifier.isFinal(field.getModifiers())) continue; + if (Modifier.isStatic(field.getModifiers())) continue; + + Map fieldmap = new LinkedHashMap<>(); + fieldmap.put("type", field.getType().getName()); + + Comment comment = field.getAnnotation(Comment.class); + if (comment != null) fieldmap.put("comment", comment.value()); + + if (servlet.getClass().getAnnotation(Rest.RestDynamic.class) != null) { + if (field.getAnnotation(RestAddress.class) != null) continue; + } + + typemap.put(field.getName(), fieldmap); + } + } while ((loop = loop.getSuperclass()) != Object.class); + + typesmap.put(ptype.getName(), typemap); } actionsList.add(actionmap); } servletsList.add(servletmap); } } + Map resultmap = new LinkedHashMap<>(); + resultmap.put("servers", serverList); + resultmap.put("types", typesmap); final FileOutputStream out = new FileOutputStream(new File(app.getHome(), "restdoc.json")); - out.write(JsonConvert.root().convertTo(serverList).getBytes("UTF-8")); + out.write(JsonConvert.root().convertTo(resultmap).getBytes("UTF-8")); out.close(); } diff --git a/src/org/redkale/net/http/Rest.java b/src/org/redkale/net/http/Rest.java index 3220369c4..a4095d132 100644 --- a/src/org/redkale/net/http/Rest.java +++ b/src/org/redkale/net/http/Rest.java @@ -6,6 +6,9 @@ package org.redkale.net.http; import java.io.*; +import java.lang.annotation.*; +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.reflect.*; import java.util.*; import jdk.internal.org.objectweb.asm.*; @@ -41,6 +44,14 @@ public final class Rest { } } + @Inherited + @Documented + @Target({TYPE}) + @Retention(RUNTIME) + public static @interface RestDynamic { + + } + private Rest() { } @@ -99,6 +110,12 @@ public final class Rest { Map classMap = new LinkedHashMap<>(); List> actionMaps = new ArrayList<>(); cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, newDynName, null, supDynName, null); + + { //RestDynamic + av0 = cw.visitAnnotation(Type.getDescriptor(RestDynamic.class), true); + av0.visitEnd(); + } + { //注入 @WebServlet 注解 String urlpath = "/" + defmodulename + "/*"; int moduleid = controller == null ? 0 : controller.module(); @@ -146,7 +163,7 @@ public final class Rest { final Map restAttributes = new LinkedHashMap<>(); for (final Method method : serviceType.getMethods()) { - if(Modifier.isStatic(method.getModifiers())) continue; + if (Modifier.isStatic(method.getModifiers())) continue; Class[] extypes = method.getExceptionTypes(); if (extypes.length > 1) continue; if (extypes.length == 1 && extypes[0] != IOException.class) continue;