@WebAction替换成@WebMapping

This commit is contained in:
Redkale
2017-03-08 18:56:07 +08:00
parent 0359a4b7e9
commit 79b91f8386
7 changed files with 92 additions and 61 deletions

View File

@@ -16,7 +16,7 @@ import org.redkale.util.*;
/**
* API接口文档生成类作用生成Application实例中所有HttpServer的可用HttpServlet的API接口方法 <br>
* 继承 HttpBaseServlet 是为了获取 WebAction 信息
继承 HttpBaseServlet 是为了获取 WebMapping 信息
*
* <p>
* 详情见: https://redkale.org
@@ -63,8 +63,8 @@ public class ApiDocs extends HttpBaseServlet {
servletmap.put("name", ws.name());
servletmap.put("comment", ws.comment());
List<Map> actionsList = new ArrayList<>();
servletmap.put("actions", actionsList);
List<Map> mappingsList = new ArrayList<>();
servletmap.put("mappings", mappingsList);
final Class selfClz = servlet.getClass();
Class clz = servlet.getClass();
HashSet<String> actionurls = new HashSet<>();
@@ -72,18 +72,18 @@ public class ApiDocs extends HttpBaseServlet {
if (Modifier.isAbstract(clz.getModifiers())) break;
for (Method method : clz.getMethods()) {
if (method.getParameterCount() != 2) continue;
WebAction action = method.getAnnotation(WebAction.class);
WebMapping action = method.getAnnotation(WebMapping.class);
if (action == null) continue;
if (!action.inherited() && selfClz != clz) continue; //忽略不被继承的方法
final Map<String, Object> actionmap = new LinkedHashMap<>();
final Map<String, Object> mappingmap = new LinkedHashMap<>();
if (actionurls.contains(action.url())) continue;
actionmap.put("url", prefix + action.url());
mappingmap.put("url", prefix + action.url());
actionurls.add(action.url());
actionmap.put("auth", method.getAnnotation(AuthIgnore.class) == null);
actionmap.put("actionid", action.actionid());
actionmap.put("comment", action.comment());
mappingmap.put("auth", method.getAnnotation(AuthIgnore.class) == null);
mappingmap.put("actionid", action.actionid());
mappingmap.put("comment", action.comment());
List<Map> paramsList = new ArrayList<>();
actionmap.put("params", paramsList);
mappingmap.put("params", paramsList);
List<String> results = new ArrayList<>();
for (final Class rtype : action.results()) {
results.add(rtype.getName());
@@ -121,7 +121,7 @@ public class ApiDocs extends HttpBaseServlet {
} while ((loop = loop.getSuperclass()) != Object.class);
typesmap.put(rtype.getName(), typemap);
}
actionmap.put("results", results);
mappingmap.put("results", results);
for (WebParam param : method.getAnnotationsByType(WebParam.class)) {
final Map<String, Object> parammap = new LinkedHashMap<>();
final boolean isarray = param.type().isArray();
@@ -171,11 +171,11 @@ public class ApiDocs extends HttpBaseServlet {
typesmap.put(ptype.getName(), typemap);
}
actionmap.put("result", action.result());
actionsList.add(actionmap);
mappingmap.put("result", action.result());
mappingsList.add(mappingmap);
}
} while ((clz = clz.getSuperclass()) != HttpServlet.class);
actionsList.sort((o1, o2) -> ((String) o1.get("url")).compareTo((String) o2.get("url")));
mappingsList.sort((o1, o2) -> ((String) o1.get("url")).compareTo((String) o2.get("url")));
servletsList.add(servletmap);
}
servletsList.sort((o1, o2) -> {

View File

@@ -36,8 +36,8 @@
if (html.length > 2) html.push(' <tr><th colspan="5" style="border-bottom:0;">&nbsp;</th></tr>');
html.push(' <tr><th colspan="5" style="border-top:' + ((html.length > 2) ? 0 : 1) + ';">' + (servlet.comment || '未知模块') + '</th></tr>');
html.push(' <tr><th>请求URL</th><th>描 述</th><th>鉴 权</th><th>参 数 <span style="font-size:12px;">(粗体: 必填项; 红色: Header; 蓝色: Cookie)</span></th><th>输 出</th></tr>');
for (var k = 0; k < servlet.actions.length; k++) {
var action = servlet.actions[k];
for (var k = 0; k < servlet.mappings.length; k++) {
var action = servlet.mappings[k];
html.push(' <tr>');
html.push('<td style="color:#ff00ff;">' + action.url + '</td>');
html.push('<td>' + action.comment + '</td>');

View File

@@ -66,8 +66,8 @@ public abstract class HttpBaseServlet extends HttpServlet {
}
/**
* 配合 &#64;WebAction 使用。
* 用于对&#64;WebAction方法中参数描述
* 配合 &#64;WebMapping 使用。
* 用于对&#64;WebMapping方法中参数描述
*
* <p>
* 详情见: https://redkale.org
@@ -101,6 +101,34 @@ public abstract class HttpBaseServlet extends HttpServlet {
WebParam[] value();
}
/**
* 使用 WebMapping 替代。
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
*/
@Deprecated
@Documented
@Target({METHOD})
@Retention(RUNTIME)
protected @interface WebAction {
int actionid() default 0;
String url();
String[] methods() default {};//允许方法(不区分大小写),如:GET/POST/PUT,为空表示允许所有方法
String comment() default ""; //备注描述
boolean inherited() default true; //是否能被继承, 当 HttpBaseServlet 被继承后该方法是否能被子类继承
String result() default "Object"; //输出结果的数据类型
Class[] results() default {}; //输出结果的数据类型集合,由于结果类型可能是泛型而注解的参数值不支持泛型,因此加入明细数据类型集合
}
/**
* 配合 HttpBaseServlet 使用。
* 用于对&#64;WebServlet对应的url进行细分。 其url必须是包含WebServlet中定义的前缀 且不能是正则表达式
@@ -113,7 +141,7 @@ public abstract class HttpBaseServlet extends HttpServlet {
@Documented
@Target({METHOD})
@Retention(RUNTIME)
protected @interface WebAction {
protected @interface WebMapping {
int actionid() default 0;
@@ -153,7 +181,7 @@ public abstract class HttpBaseServlet extends HttpServlet {
int seconds() default 15;
}
private Map.Entry<String, Entry>[] actions;
private Map.Entry<String, Entry>[] mappings;
public boolean preExecute(HttpRequest request, HttpResponse response) throws IOException {
return true;
@@ -162,7 +190,7 @@ public abstract class HttpBaseServlet extends HttpServlet {
@Override
public final void execute(HttpRequest request, HttpResponse response) throws IOException {
if (!preExecute(request, response)) return;
for (Map.Entry<String, Entry> en : actions) {
for (Map.Entry<String, Entry> en : mappings) {
if (request.getRequestURI().startsWith(en.getKey())) {
Entry entry = en.getValue();
if (!entry.checkMethod(request.getMethod())) {
@@ -193,13 +221,13 @@ public abstract class HttpBaseServlet extends HttpServlet {
WebServlet ws = this.getClass().getAnnotation(WebServlet.class);
if (ws != null && !ws.repair()) path = "";
HashMap<String, Entry> map = load();
this.actions = new Map.Entry[map.size()];
this.mappings = new Map.Entry[map.size()];
int i = -1;
for (Map.Entry<String, Entry> en : map.entrySet()) {
actions[++i] = new AbstractMap.SimpleEntry<>(path + en.getKey(), en.getValue());
mappings[++i] = new AbstractMap.SimpleEntry<>(path + en.getKey(), en.getValue());
}
//必须要倒排序, /query /query1 /query12 确保含子集的优先匹配 /query12 /query1 /query
Arrays.sort(actions, (o1, o2) -> o2.getKey().compareTo(o1.getKey()));
Arrays.sort(mappings, (o1, o2) -> o2.getKey().compareTo(o1.getKey()));
}
public final void postDestroy(HttpContext context, AnyValue config) {
@@ -242,17 +270,20 @@ public abstract class HttpBaseServlet extends HttpServlet {
if (exps.length > 0 && (exps.length != 1 || exps[0] != IOException.class)) continue;
//-----------------------------------------------
final WebMapping mapping = method.getAnnotation(WebMapping.class);
final WebAction action = method.getAnnotation(WebAction.class);
if (action == null) continue;
if (!action.inherited() && selfClz != clz) continue; //忽略不被继承的方法
final int actionid = action.actionid();
final String name = action.url().trim();
if (mapping == null && action == null) continue;
final boolean inherited = action == null ? action.inherited() : mapping.inherited();
if (!inherited && selfClz != clz) continue; //忽略不被继承的方法
final int actionid = action == null ? action.actionid() : mapping.actionid();
final String name = action == null ? action.url().trim() : mapping.url().trim();
final String[] methods = action == null ? action.methods() : mapping.methods();
if (nameset.containsKey(name)) {
if (nameset.get(name) != clz) continue;
throw new RuntimeException(this.getClass().getSimpleName() + " has two same " + WebAction.class.getSimpleName() + "(" + name + ")");
throw new RuntimeException(this.getClass().getSimpleName() + " has two same " + WebMapping.class.getSimpleName() + "(" + name + ")");
}
nameset.put(name, clz);
map.put(name, new Entry(typeIgnore, serviceid, actionid, name, action.methods(), method, createHttpServlet(method)));
map.put(name, new Entry(typeIgnore, serviceid, actionid, name, methods, method, createHttpServlet(method)));
}
} while ((clz = clz.getSuperclass()) != HttpBaseServlet.class);
return map;

View File

@@ -82,7 +82,7 @@ public final class Rest {
final String attrDesc = Type.getDescriptor(org.redkale.util.Attribute.class);
final String authDesc = Type.getDescriptor(HttpBaseServlet.AuthIgnore.class);
final String cacheDesc = Type.getDescriptor(HttpBaseServlet.HttpCacheable.class);
final String actionDesc = Type.getDescriptor(HttpBaseServlet.WebAction.class);
final String mappingDesc = Type.getDescriptor(HttpBaseServlet.WebMapping.class);
final String webparamDesc = Type.getDescriptor(HttpBaseServlet.WebParam.class);
final String webparamsDesc = Type.getDescriptor(HttpBaseServlet.WebParams.class);
final String sourcetypeDesc = Type.getDescriptor(HttpBaseServlet.ParamSourceType.class);
@@ -122,7 +122,7 @@ public final class Rest {
AsmMethodVisitor mv;
AnnotationVisitor av0;
Map<String, Object> classMap = new LinkedHashMap<>();
List<Map<String, Object>> actionMaps = new ArrayList<>();
List<Map<String, Object>> mappingMaps = new ArrayList<>();
cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, newDynName, null, supDynName, null);
{ //RestDynamic
@@ -207,7 +207,7 @@ public final class Rest {
}
}
}
if (entrys.isEmpty()) return null; //没有可WebAction的方法
if (entrys.isEmpty()) return null; //没有可WebMapping的方法
for (final MappingEntry entry : entrys) {
final Method method = entry.mappingMethod;
final Class returnType = method.getReturnType();
@@ -316,9 +316,9 @@ public final class Rest {
paramlist.add(new Object[]{param, n, ptype, radix, comment, required, annpara, annsid, annaddr, annhead, anncookie});
}
Map<String, Object> actionMap = new LinkedHashMap<>();
Map<String, Object> mappingMap = new LinkedHashMap<>();
{
//设置 WebAction
//设置 WebMapping
boolean reqpath = false;
for (Object[] ps : paramlist) {
if ("#".equals((String) ps[1])) {
@@ -326,7 +326,7 @@ public final class Rest {
break;
}
}
av0 = mv.visitAnnotation(actionDesc, true);
av0 = mv.visitAnnotation(mappingDesc, true);
String url = "/" + defmodulename.toLowerCase() + "/" + entry.name + (reqpath ? "/" : "");
av0.visit("url", url);
av0.visit("actionid", entry.actionid);
@@ -342,13 +342,13 @@ public final class Rest {
av0.visit("result", grt == returnType ? returnType.getName() : String.valueOf(grt));
av0.visitEnd();
actionMap.put("url", url);
actionMap.put("auth", entry.auth);
actionMap.put("cachetimeout", entry.cacheseconds);
actionMap.put("actionid", entry.actionid);
actionMap.put("comment", entry.comment);
actionMap.put("methods", entry.methods);
actionMap.put("result", grt == returnType ? returnType.getName() : String.valueOf(grt));
mappingMap.put("url", url);
mappingMap.put("auth", entry.auth);
mappingMap.put("cachetimeout", entry.cacheseconds);
mappingMap.put("actionid", entry.actionid);
mappingMap.put("comment", entry.comment);
mappingMap.put("methods", entry.methods);
mappingMap.put("result", grt == returnType ? returnType.getName() : String.valueOf(grt));
}
{
@@ -838,8 +838,8 @@ public final class Rest {
maxLocals++;
}
mv.visitMaxs(maxStack, maxLocals);
actionMap.put("params", paramMaps);
actionMaps.add(actionMap);
mappingMap.put("params", paramMaps);
mappingMaps.add(mappingMap);
} // end for each
for (String attrname : restAttributes.keySet()) {
@@ -847,7 +847,7 @@ public final class Rest {
fv.visitEnd();
}
classMap.put("actions", actionMaps);
classMap.put("mappings", mappingMaps);
{ //toString函数
mv = new AsmMethodVisitor(cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null));

View File

@@ -40,7 +40,7 @@ public @interface RestMapping {
String name() default "";
/**
* 备注描述, 对应&#64;WebAction.comment
* 备注描述, 对应&#64;WebMapping.comment
*
* @return String
*/
@@ -54,7 +54,7 @@ public @interface RestMapping {
boolean auth() default false;
/**
* 操作ID值鉴权时用到, 对应&#64;WebAction.actionid
* 操作ID值鉴权时用到, 对应&#64;WebMapping.actionid
*
* @return int
*/
@@ -68,7 +68,7 @@ public @interface RestMapping {
int cacheseconds() default 0;
/**
* 允许方法(不区分大小写),如:GET/POST/PUT,为空表示允许所有方法, 对应&#64;WebAction.methods
* 允许方法(不区分大小写),如:GET/POST/PUT,为空表示允许所有方法, 对应&#64;WebMapping.methods
*
* @return String[]
*/

View File

@@ -42,7 +42,7 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
}
@AuthIgnore
@WebAction(url = "/hello/create")
@WebMapping(url = "/hello/create")
public void create(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _servicemap == null ? _service : _servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, ""));
HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean");
@@ -54,7 +54,7 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
}
@AuthIgnore
@WebAction(url = "/hello/delete/")
@WebMapping(url = "/hello/delete/")
public void delete(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _servicemap == null ? _service : _servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, ""));
int id = Integer.parseInt(req.getRequstURILastPath());
@@ -63,7 +63,7 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
}
@AuthIgnore
@WebAction(url = "/hello/update")
@WebMapping(url = "/hello/update")
public void update(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _servicemap == null ? _service : _servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, ""));
String clientaddr = req.getRemoteAddr();
@@ -75,7 +75,7 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
}
@AuthIgnore
@WebAction(url = "/hello/partupdate")
@WebMapping(url = "/hello/partupdate")
public void partupdate(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _servicemap == null ? _service : _servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, ""));
HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean");
@@ -87,7 +87,7 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
}
@AuthIgnore
@WebAction(url = "/hello/query")
@WebMapping(url = "/hello/query")
public void query(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _servicemap == null ? _service : _servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, ""));
HelloBean bean = req.getJsonParameter(HelloBean.class, "bean");
@@ -101,7 +101,7 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
}
@AuthIgnore
@WebAction(url = "/hello/list")
@WebMapping(url = "/hello/list")
public void list(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _servicemap == null ? _service : _servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, ""));
HelloBean bean = req.getJsonParameter(HelloBean.class, "bean");
@@ -114,7 +114,7 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
}
@AuthIgnore
@WebAction(url = "/hello/find/")
@WebMapping(url = "/hello/find/")
public void find(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _servicemap == null ? _service : _servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, ""));
int id = Integer.parseInt(req.getRequstURILastPath());

View File

@@ -27,7 +27,7 @@ public class _DynHelloRestServlet2 extends SimpleRestServlet {
private Map<String, HelloService2> _servicemap;
@AuthIgnore
@WebAction(url = "/hello/create", comment = "创建Hello对象")
@WebMapping(url = "/hello/create", comment = "创建Hello对象")
@WebParam(name = "bean", type = HelloEntity.class, comment = "Hello对象")
public void create(HttpRequest req, HttpResponse resp) throws IOException {
HelloService2 service = _servicemap == null ? _service : _servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, ""));
@@ -40,7 +40,7 @@ public class _DynHelloRestServlet2 extends SimpleRestServlet {
}
@AuthIgnore
@WebAction(url = "/hello/delete/", comment = "根据id删除Hello对象")
@WebMapping(url = "/hello/delete/", comment = "根据id删除Hello对象")
@WebParam(name = "#", type = int.class, comment = "Hello对象id")
public void delete(HttpRequest req, HttpResponse resp) throws IOException {
HelloService2 service = _servicemap == null ? _service : _servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, ""));
@@ -50,7 +50,7 @@ public class _DynHelloRestServlet2 extends SimpleRestServlet {
}
@AuthIgnore
@WebAction(url = "/hello/update", comment = "修改Hello对象")
@WebMapping(url = "/hello/update", comment = "修改Hello对象")
@WebParam(name = "bean", type = HelloEntity.class, comment = "Hello对象")
public void update(HttpRequest req, HttpResponse resp) throws IOException {
HelloService2 service = _servicemap == null ? _service : _servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, ""));
@@ -62,7 +62,7 @@ public class _DynHelloRestServlet2 extends SimpleRestServlet {
}
@AuthIgnore
@WebAction(url = "/hello/query", comment = "查询Hello对象列表")
@WebMapping(url = "/hello/query", comment = "查询Hello对象列表")
@WebParam(name = "bean", type = HelloBean.class, comment = "过滤条件")
public void query(HttpRequest req, HttpResponse resp) throws IOException {
HelloService2 service = _servicemap == null ? _service : _servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, ""));
@@ -77,7 +77,7 @@ public class _DynHelloRestServlet2 extends SimpleRestServlet {
}
@AuthIgnore
@WebAction(url = "/hello/find/", comment = "根据id删除Hello对象")
@WebMapping(url = "/hello/find/", comment = "根据id删除Hello对象")
@WebParam(name = "#", type = int.class, comment = "Hello对象id")
public void find(HttpRequest req, HttpResponse resp) throws IOException {
HelloService2 service = _servicemap == null ? _service : _servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, ""));