This commit is contained in:
Redkale
2017-01-09 10:38:34 +08:00
parent ae9aa94323
commit 606faf1bf8
2 changed files with 62 additions and 50 deletions

View File

@@ -62,12 +62,20 @@ public class ApiDocs extends HttpBaseServlet {
List<Map> actionsList = new ArrayList<>(); List<Map> actionsList = new ArrayList<>();
servletmap.put("actions", actionsList); servletmap.put("actions", actionsList);
for (Method method : servlet.getClass().getMethods()) { final Class selfClz = servlet.getClass();
Class clz = servlet.getClass();
HashSet<String> actionurls = new HashSet<>();
do {
if (Modifier.isAbstract(clz.getModifiers())) break;
for (Method method : clz.getMethods()) {
if (method.getParameterCount() != 2) continue; if (method.getParameterCount() != 2) continue;
WebAction action = method.getAnnotation(WebAction.class); WebAction action = method.getAnnotation(WebAction.class);
if (action == null) continue; if (action == null) continue;
if (!action.inherited() && selfClz != clz) continue; //忽略不被继承的方法
final Map<String, Object> actionmap = new LinkedHashMap<>(); final Map<String, Object> actionmap = new LinkedHashMap<>();
if (actionurls.contains(action.url())) continue;
actionmap.put("url", prefix + action.url()); actionmap.put("url", prefix + action.url());
actionurls.add(action.url());
actionmap.put("auth", method.getAnnotation(AuthIgnore.class) == null); actionmap.put("auth", method.getAnnotation(AuthIgnore.class) == null);
actionmap.put("actionid", action.actionid()); actionmap.put("actionid", action.actionid());
actionmap.put("comment", action.comment()); actionmap.put("comment", action.comment());
@@ -118,6 +126,7 @@ public class ApiDocs extends HttpBaseServlet {
actionmap.put("result", action.result()); actionmap.put("result", action.result());
actionsList.add(actionmap); actionsList.add(actionmap);
} }
} while ((clz = clz.getSuperclass()) != HttpServlet.class);
actionsList.sort((o1, o2) -> ((String) o1.get("url")).compareTo((String) o2.get("url"))); actionsList.sort((o1, o2) -> ((String) o1.get("url")).compareTo((String) o2.get("url")));
servletsList.add(servletmap); servletsList.add(servletmap);
} }

View File

@@ -123,6 +123,7 @@ public abstract class HttpBaseServlet extends HttpServlet {
String result() default "Object"; //输出结果的数据类型 String result() default "Object"; //输出结果的数据类型
boolean inherited() default true; //是否能被继承, 当 HttpBaseServlet 被继承后该方法是否能被子类继承
} }
/** /**
@@ -220,6 +221,7 @@ public abstract class HttpBaseServlet extends HttpServlet {
final int serviceid = module == null ? 0 : module.moduleid(); final int serviceid = module == null ? 0 : module.moduleid();
final HashMap<String, Entry> map = new HashMap<>(); final HashMap<String, Entry> map = new HashMap<>();
HashMap<String, Class> nameset = new HashMap<>(); HashMap<String, Class> nameset = new HashMap<>();
final Class selfClz = this.getClass();
Class clz = this.getClass(); Class clz = this.getClass();
do { do {
if (Modifier.isAbstract(clz.getModifiers())) break; if (Modifier.isAbstract(clz.getModifiers())) break;
@@ -238,6 +240,7 @@ public abstract class HttpBaseServlet extends HttpServlet {
final WebAction action = method.getAnnotation(WebAction.class); final WebAction action = method.getAnnotation(WebAction.class);
if (action == null) continue; if (action == null) continue;
if (!action.inherited() && selfClz != clz) continue; //忽略不被继承的方法
final int actionid = action.actionid(); final int actionid = action.actionid();
final String name = action.url().trim(); final String name = action.url().trim();
if (nameset.containsKey(name)) { if (nameset.containsKey(name)) {