From 746799310448d31a64307d02d216c3af01707f93 Mon Sep 17 00:00:00 2001 From: wentch <22250530@qq.com> Date: Thu, 14 Jan 2016 15:17:39 +0800 Subject: [PATCH] --- convert.html | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/convert.html b/convert.html index 8b2acc84f..853ef190e 100644 --- a/convert.html +++ b/convert.html @@ -137,6 +137,91 @@ @Resource private BsonConvert bsonConvert; +} + +

      同一类型数据通过设置新的JsonFactory可以有不同的输出。

+
public class UserSimpleInfo {
+
+    private int userid;
+
+    private String username = "";
+
+    private long regtime; //注册时间
+
+    private String regaddr = ""; //注册IP
+
+    @ConvertColumn(ignore = true, type = ConvertType.JSON)
+    public long getRegtime() {
+        return regtime;
+    }
+
+    public void setRegtime(long regtime) {
+        this.regtime = regtime;
+    }
+
+    @ConvertColumn(ignore = true, type = ConvertType.JSON)
+    public String getRegaddr() {
+        return regaddr;
+    }
+
+    public void setRegaddr(String regaddr) {
+        this.regaddr = regaddr;
+    }
+
+    public int getUserid() {
+        return userid;
+    }
+
+    public void setUserid(int userid) {
+        this.userid = userid;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+}
+
+
+
+public class UserInfoServlet extends BasedHttpServlet {
+
+    @Resource
+    private UserSerice service;
+
+    @Resource
+    private JsonFactory jsonRootFactory;
+
+    @Resource
+    private JsonConvert detailConvert;
+
+    @Override
+    public void init(Context context, AnyValue config) {
+        final JsonFactory childFactory = jsonRootFactory.createChild();
+        childFactory.register(UserSimpleInfo.class, false, "regtime", "regaddr"); //允许输出注册时间与注册地址
+        childFactory.reloadCoder(UserSimpleInfo.class); //重新加载Coder使之覆盖父Factory的配置
+        this.detailConvert = childFactory.getConvert();
+    }
+
+    // 获取他人基本信息
+    @AuthIgnore
+    @WebAction(url = "/user/info/")
+    public void info(HttpRequest req, HttpResponse resp) throws IOException {
+        int userid = Integer.parseInt(req.getRequstURILastPath());
+        UserSimpleInfo user = service.findUserInfo(userid);
+        resp.finishJson(user);  // 不包含用户的注册时间和注册地址字段信息
+    }
+
+    //获取用户自己的信息
+    @WebAction(url = "/user/myinfo")
+    public void mydetail(HttpRequest req, HttpResponse resp) throws IOException {
+        int userid = currentUser(req).getUserid(); //获取当前用户ID
+        UserSimpleInfo user = service.findUserInfo(userid);
+        resp.finishJson(detailConvert, req);  // 包含用户的注册时间和注册地址字段信息
+    }
 }