From bc1c74915271c3eabd68e9b0846cc6aa626bb4b1 Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Wed, 14 Sep 2016 10:39:56 +0800 Subject: [PATCH] --- src/org/redkale/net/http/HttpBaseServlet.java | 4 ++-- src/org/redkale/net/http/Rest.java | 18 ++++++++++++++---- src/org/redkale/net/http/RestMapping.java | 4 +++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/org/redkale/net/http/HttpBaseServlet.java b/src/org/redkale/net/http/HttpBaseServlet.java index 88fd18da8..98f76f01e 100644 --- a/src/org/redkale/net/http/HttpBaseServlet.java +++ b/src/org/redkale/net/http/HttpBaseServlet.java @@ -120,8 +120,8 @@ public abstract class HttpBaseServlet extends HttpServlet { /** * 配合 HttpBaseServlet 使用。 - * 当标记为 @HttpCacheable 的方法使用response.finish的参数将被缓存一定时间(默认值timeout=15秒)。 - * 通常情况下 @HttpCacheable 需要与 @AuthIgnore 一起使用,因为没有标记@AuthIgnore的方法一般输出的结果与当前用户信息有关。 + * 当标记为 @HttpCacheable 的方法使用response.finish的参数将被缓存一段时间(默认值timeout=15秒)。 + * 通常情况下 @HttpCacheable 需要与 @AuthIgnore 一起使用,没有标记@AuthIgnore的方法一般输出的结果与当前用户信息有关。 * *

* 详情见: http://redkale.org diff --git a/src/org/redkale/net/http/Rest.java b/src/org/redkale/net/http/Rest.java index 11a8698db..c2f5fdd8a 100644 --- a/src/org/redkale/net/http/Rest.java +++ b/src/org/redkale/net/http/Rest.java @@ -76,6 +76,7 @@ public final class Rest { final String restoutputDesc = Type.getDescriptor(RestOutput.class); 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 webparamDesc = Type.getDescriptor(HttpBaseServlet.WebParam.class); final String sourcetypeDesc = Type.getDescriptor(HttpBaseServlet.ParamSourceType.class); @@ -207,6 +208,11 @@ public final class Rest { av0 = mv.visitAnnotation(authDesc, true); av0.visitEnd(); } + if (entry.cachetimeout > 0) { //设置 HttpCacheable + av0 = mv.visitAnnotation(cacheDesc, true); + av0.visit("timeout", entry.cachetimeout); + av0.visitEnd(); + } mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, newDynName, REST_SERVICEMAP_FIELD_NAME, "Ljava/util/Map;"); @@ -340,14 +346,16 @@ public final class Rest { av3.visitEnd(); } java.lang.reflect.Type grt = method.getGenericReturnType(); - av0.visit("result", grt == returnType ? returnType.getName(): String.valueOf(grt)); + 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.cachetimeout); 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)); + actionMap.put("result", grt == returnType ? returnType.getName() : String.valueOf(grt)); } List> paramMaps = new ArrayList<>(); @@ -1025,7 +1033,8 @@ public final class Rest { this.methods = mapping.methods(); this.auth = mapping.auth(); this.actionid = mapping.actionid(); - this.contentType = mapping.contentType(); + this.cachetimeout = mapping.cachetimeout(); + //this.contentType = mapping.contentType(); this.comment = mapping.comment(); this.jsvar = mapping.jsvar(); } @@ -1044,8 +1053,9 @@ public final class Rest { public final int actionid; - public final String contentType; + public final int cachetimeout; + //public final String contentType; public final String jsvar; @RestMapping() diff --git a/src/org/redkale/net/http/RestMapping.java b/src/org/redkale/net/http/RestMapping.java index da4ce420c..35774a096 100644 --- a/src/org/redkale/net/http/RestMapping.java +++ b/src/org/redkale/net/http/RestMapping.java @@ -41,9 +41,11 @@ public @interface RestMapping { int actionid() default 0; //操作ID值,鉴权时用到, 对应@WebAction.actionid + int cachetimeout() default 0; // 结果缓存的秒数, 为0表示不缓存 + String[] methods() default {};//允许方法(不区分大小写),如:GET/POST/PUT,为空表示允许所有方法, 对应@WebAction.methods - String contentType() default ""; //设置Response的ContentType 默认值为 text/plain; charset=utf-8 + //String contentType() default ""; //设置Response的ContentType 默认值为 text/plain; charset=utf-8 String jsvar() default ""; //以application/javascript输出对象是指明js的对象名,该值存在时则忽略contentType()的值 }