From 1cfee917f3b62d2ec96bc8814eceff1c76feee02 Mon Sep 17 00:00:00 2001 From: mashiyu Date: Thu, 17 Jan 2019 15:20:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0jar=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../controller/GetServerListController.java | 1 + .../ljsd/controller/ServerListController.java | 12 +--------- .../java/com/ljsd/pojo/UserServerInfo.java | 11 +++++++++ src/main/java/com/ljsd/redis/RedisApp.java | 23 ++++++++++--------- 5 files changed, 26 insertions(+), 23 deletions(-) diff --git a/build.gradle b/build.gradle index 466ee7a..a0f243c 100644 --- a/build.gradle +++ b/build.gradle @@ -17,5 +17,5 @@ dependencies { compile("ch.qos.logback:logback-core:1.1.11") compile("ch.qos.logback:logback-classic:1.1.11") compile group: 'redis.clients', name: 'jedis', version: '2.9.0' - compile group: 'com.alibaba', name: 'fastjson', version: '1.2.47' + compile group: 'com.google.code.gson', name: 'gson', version: '2.2.4' } diff --git a/src/main/java/com/ljsd/controller/GetServerListController.java b/src/main/java/com/ljsd/controller/GetServerListController.java index a19c5b9..9732222 100644 --- a/src/main/java/com/ljsd/controller/GetServerListController.java +++ b/src/main/java/com/ljsd/controller/GetServerListController.java @@ -68,6 +68,7 @@ public class GetServerListController extends HttpServlet { DBObject res = new BasicDBObject(); res.put("server_id", entry.getKey()); res.put("login_time", entry.getValue().getLoginTime()); + res.put("level", entry.getValue().getLevel()); myServerList.add(res); } DBObject resp = new BasicDBObject(); diff --git a/src/main/java/com/ljsd/controller/ServerListController.java b/src/main/java/com/ljsd/controller/ServerListController.java index 77f20f9..5f7c82a 100644 --- a/src/main/java/com/ljsd/controller/ServerListController.java +++ b/src/main/java/com/ljsd/controller/ServerListController.java @@ -1,24 +1,14 @@ package com.ljsd.controller; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; -import com.ljsd.pojo.UserServerInfo; -import com.ljsd.redis.RedisKey; + import com.ljsd.util.BaseGlobal; -import com.mongodb.BasicDBObject; -import com.mongodb.DBObject; -import com.mongodb.util.JSON; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; public class ServerListController extends HttpServlet{ private String message; diff --git a/src/main/java/com/ljsd/pojo/UserServerInfo.java b/src/main/java/com/ljsd/pojo/UserServerInfo.java index 6aae8dc..5a3f172 100644 --- a/src/main/java/com/ljsd/pojo/UserServerInfo.java +++ b/src/main/java/com/ljsd/pojo/UserServerInfo.java @@ -1,8 +1,11 @@ package com.ljsd.pojo; public class UserServerInfo { + private long loginTime; + private int level; + public long getLoginTime() { return loginTime; } @@ -10,4 +13,12 @@ public class UserServerInfo { public void setLoginTime(long loginTime) { this.loginTime = loginTime; } + + public int getLevel() { + return level; + } + + public void setLevel(int level) { + this.level = level; + } } diff --git a/src/main/java/com/ljsd/redis/RedisApp.java b/src/main/java/com/ljsd/redis/RedisApp.java index 6544346..9e2c1f9 100644 --- a/src/main/java/com/ljsd/redis/RedisApp.java +++ b/src/main/java/com/ljsd/redis/RedisApp.java @@ -1,12 +1,13 @@ package com.ljsd.redis; -import com.alibaba.fastjson.JSON; +import com.google.gson.Gson; import java.util.*; public class RedisApp { private final static String Delimiter_colon = ":"; private RedisUtil redisUtil = new RedisUtil(); + private Gson gson = new Gson(); private String areaId = "0"; public RedisApp() { @@ -24,7 +25,7 @@ public class RedisApp { /***************************************普通键值对操作***************************************/ //设置键值 public void set(final String type, final K key, T value, final int seconds, boolean withServerId) throws Exception { - String strValue = JSON.toJSONString(value); + String strValue = gson.toJson(value); String fullKey = getKey(type, key, withServerId); if (seconds <= 0) { redisUtil.set(fullKey, strValue); @@ -45,7 +46,7 @@ public class RedisApp { if (strValue == null) { return null; } - return JSON.parseObject(strValue, clazz); + return gson.fromJson(strValue,clazz); } public T get(final String type, final K key, final Class clazz, final int seconds) throws Exception { @@ -87,7 +88,7 @@ public class RedisApp { Map map = new HashMap(); for (Map.Entry entry : mapValues.entrySet()) { String strKey = String.valueOf(entry.getKey()); - String strValue = JSON.toJSONString(entry.getValue()); + String strValue = gson.toJson(entry.getValue()); map.put(strKey, strValue); } return map; @@ -119,7 +120,7 @@ public class RedisApp { public void hset(final String type, final K key, final String field, final T value, int seconds, boolean withServerId) throws Exception { String fullKey = getKey(type, key, withServerId); - String strValue = JSON.toJSONString(value); + String strValue = gson.toJson(value); redisUtil.hset(fullKey, field, strValue, seconds); } @@ -131,7 +132,7 @@ public class RedisApp { private Map converClassMap(final Map mapValues, final Class valueClazz) { Map map = new HashMap<>(); for (Map.Entry entry : mapValues.entrySet()) { - T value = JSON.parseObject(entry.getValue(), valueClazz); + T value = gson.fromJson(entry.getValue(), valueClazz); map.put(entry.getKey(), value); } return map; @@ -160,7 +161,7 @@ public class RedisApp { if (listStrs == null || listStrs.size() <= 0) { return null; } - return JSON.parseObject(listStrs.get(0), clazz); + return gson.fromJson(listStrs.get(0), clazz); } public T hmget(final String type, final K key, final SK field, final Class clazz, int seconds) throws Exception { @@ -185,7 +186,7 @@ public class RedisApp { String fullKey = getKey(type, key, withServerId); String[] strValues = new String[strings.length]; for (int i = 0; i < strings.length; i++) { - strValues[i] = JSON.toJSONString(strings[i]); + strValues[i] = gson.toJson(strings[i]); } return redisUtil.lpush(fullKey, seconds, strValues); } @@ -205,7 +206,7 @@ public class RedisApp { public T rpop(final String type, final K key, final Class clazz, boolean withServerId) throws Exception { String fullKey = getKey(type, key, withServerId); String strValue = redisUtil.rpop(fullKey); - return JSON.parseObject(strValue, clazz); + return gson.fromJson(strValue, clazz); } public T rpop(final String type, final K key, final Class clazz) throws Exception { @@ -222,7 +223,7 @@ public class RedisApp { redisUtil.rpop(fullKey); } } - String strValue = JSON.toJSONString(value); + String strValue = gson.toJson(value); return redisUtil.lpush(fullKey, seconds, strValue); } @@ -234,7 +235,7 @@ public class RedisApp { private List converClassList(final List listValues, final Class clazz) { List list = new ArrayList<>(); for (String strValue : listValues) { - T value = JSON.parseObject(strValue, clazz); + T value = gson.fromJson(strValue, clazz); list.add(value); }