正在显示
4 个修改的文件
包含
73 行增加
和
18 行删除
@@ -144,12 +144,18 @@ | @@ -144,12 +144,18 @@ | ||
144 | <artifactId>swagger-models</artifactId> | 144 | <artifactId>swagger-models</artifactId> |
145 | <version>1.5.22</version> | 145 | <version>1.5.22</version> |
146 | </dependency> | 146 | </dependency> |
147 | - | 147 | + <!-- 添加Gson依赖 |
148 | <dependency> | 148 | <dependency> |
149 | <groupId>com.alibaba</groupId> | 149 | <groupId>com.alibaba</groupId> |
150 | <artifactId>fastjson</artifactId> | 150 | <artifactId>fastjson</artifactId> |
151 | <version>1.2.49</version> | 151 | <version>1.2.49</version> |
152 | </dependency> | 152 | </dependency> |
153 | + --> | ||
154 | + <!-- 添加Gson依赖 --> | ||
155 | + <dependency> | ||
156 | + <groupId>com.google.code.gson</groupId> | ||
157 | + <artifactId>gson</artifactId> | ||
158 | + </dependency> | ||
153 | <dependency> | 159 | <dependency> |
154 | <groupId>joda-time</groupId> | 160 | <groupId>joda-time</groupId> |
155 | <artifactId>joda-time</artifactId> | 161 | <artifactId>joda-time</artifactId> |
1 | package com.sunyo.wlpt.cgonms.provide.controller; | 1 | package com.sunyo.wlpt.cgonms.provide.controller; |
2 | 2 | ||
3 | -import com.alibaba.fastjson.JSON; | ||
4 | -import com.alibaba.fastjson.serializer.SerializerFeature; | ||
5 | 3 | ||
4 | + | ||
5 | +import com.google.gson.Gson; | ||
6 | +import com.google.gson.GsonBuilder; | ||
6 | import com.sunyo.wlpt.cgonms.provide.domain.*; | 7 | import com.sunyo.wlpt.cgonms.provide.domain.*; |
7 | import com.sunyo.wlpt.cgonms.provide.feign.GetCgoAsmFeign; | 8 | import com.sunyo.wlpt.cgonms.provide.feign.GetCgoAsmFeign; |
8 | import com.sunyo.wlpt.cgonms.provide.feign.GetDataWareHouseFeign; | 9 | import com.sunyo.wlpt.cgonms.provide.feign.GetDataWareHouseFeign; |
@@ -11,6 +12,7 @@ import com.sunyo.wlpt.cgonms.provide.response.ResultJson; | @@ -11,6 +12,7 @@ import com.sunyo.wlpt.cgonms.provide.response.ResultJson; | ||
11 | import com.sunyo.wlpt.cgonms.provide.response.ResultWs; | 12 | import com.sunyo.wlpt.cgonms.provide.response.ResultWs; |
12 | import com.sunyo.wlpt.cgonms.provide.service.*; | 13 | import com.sunyo.wlpt.cgonms.provide.service.*; |
13 | import com.sunyo.wlpt.cgonms.provide.thread.ExitThreadPoolFactory; | 14 | import com.sunyo.wlpt.cgonms.provide.thread.ExitThreadPoolFactory; |
15 | +import com.sunyo.wlpt.cgonms.provide.utils.GsonUtils; | ||
14 | import com.sunyo.wlpt.cgonms.provide.websocket.WebSocketServer; | 16 | import com.sunyo.wlpt.cgonms.provide.websocket.WebSocketServer; |
15 | import lombok.extern.slf4j.Slf4j; | 17 | import lombok.extern.slf4j.Slf4j; |
16 | import org.springframework.beans.propertyeditors.CustomDateEditor; | 18 | import org.springframework.beans.propertyeditors.CustomDateEditor; |
@@ -95,7 +97,8 @@ public class NmsController { | @@ -95,7 +97,8 @@ public class NmsController { | ||
95 | String sid = request.getHeader("Authorization"); | 97 | String sid = request.getHeader("Authorization"); |
96 | log.info("token的值:" + sid); | 98 | log.info("token的值:" + sid); |
97 | final String startTime = sdf.format(new Date()); | 99 | final String startTime = sdf.format(new Date()); |
98 | - String resultStart = JSON.toJSONString(new ResultWs("数据开始获取")); | 100 | + |
101 | + String resultStart = GsonUtils.toJsonStr(new ResultWs("数据开始获取")); | ||
99 | sendMsgByWebsocket(resultStart, sid); | 102 | sendMsgByWebsocket(resultStart, sid); |
100 | ResultJson resultJson = new ResultJson(); | 103 | ResultJson resultJson = new ResultJson(); |
101 | 104 | ||
@@ -106,9 +109,9 @@ public class NmsController { | @@ -106,9 +109,9 @@ public class NmsController { | ||
106 | resultExitData.setFlightDate(flightDate); | 109 | resultExitData.setFlightDate(flightDate); |
107 | resultExitData.setFlightNo(flightNo); | 110 | resultExitData.setFlightNo(flightNo); |
108 | List<ResultExitData> resultList = resultExitDataService.getResultExitDataInfo(resultExitData); | 111 | List<ResultExitData> resultList = resultExitDataService.getResultExitDataInfo(resultExitData); |
109 | - System.out.println("长度为:" + resultList.size()); | 112 | + log.info("长度为:" + resultList.size()); |
110 | ResultWs resultTotal = new ResultWs("一共" + resultList.size() + "条数据", resultList.size()); | 113 | ResultWs resultTotal = new ResultWs("一共" + resultList.size() + "条数据", resultList.size()); |
111 | - String resultTotalJs = JSON.toJSONString(resultTotal, SerializerFeature.DisableCircularReferenceDetect); | 114 | + String resultTotalJs = GsonUtils.toJsonStr(resultTotal); |
112 | sendMsgByWebsocket(resultTotalJs, sid); | 115 | sendMsgByWebsocket(resultTotalJs, sid); |
113 | 116 | ||
114 | //控制index,查询数据的时候,保证索引的位置不出错 | 117 | //控制index,查询数据的时候,保证索引的位置不出错 |
@@ -129,7 +132,7 @@ public class NmsController { | @@ -129,7 +132,7 @@ public class NmsController { | ||
129 | 132 | ||
130 | int temp = i + index; | 133 | int temp = i + index; |
131 | ResultWs resultWs = new ResultWs("获取数据,第" + temp + "条", "201", temp, resultList.size()); | 134 | ResultWs resultWs = new ResultWs("获取数据,第" + temp + "条", "201", temp, resultList.size()); |
132 | - String resultJs = JSON.toJSONString(resultWs, SerializerFeature.DisableCircularReferenceDetect); | 135 | + String resultJs = GsonUtils.toJsonStr(resultWs); |
133 | 136 | ||
134 | sendMsgByWebsocket(resultJs, sid); | 137 | sendMsgByWebsocket(resultJs, sid); |
135 | } | 138 | } |
@@ -149,7 +152,7 @@ public class NmsController { | @@ -149,7 +152,7 @@ public class NmsController { | ||
149 | int temp = i + index; | 152 | int temp = i + index; |
150 | ResultWs resultWs = new ResultWs("获取数据,第" + temp + "条", "201", temp, resultList.size()); | 153 | ResultWs resultWs = new ResultWs("获取数据,第" + temp + "条", "201", temp, resultList.size()); |
151 | 154 | ||
152 | - String resultJs = JSON.toJSONString(resultWs, SerializerFeature.DisableCircularReferenceDetect); | 155 | + String resultJs = GsonUtils.toJsonStr(resultWs); |
153 | sendMsgByWebsocket(resultJs, sid); | 156 | sendMsgByWebsocket(resultJs, sid); |
154 | } | 157 | } |
155 | index = index + i; | 158 | index = index + i; |
@@ -168,7 +171,7 @@ public class NmsController { | @@ -168,7 +171,7 @@ public class NmsController { | ||
168 | 171 | ||
169 | resultList.set(i, result); | 172 | resultList.set(i, result); |
170 | ResultWs resultWs = new ResultWs("获取数据,第" + i + "条", "201", i, resultList.size()); | 173 | ResultWs resultWs = new ResultWs("获取数据,第" + i + "条", "201", i, resultList.size()); |
171 | - String resultJs = JSON.toJSONString(resultWs, SerializerFeature.DisableCircularReferenceDetect); | 174 | + String resultJs = GsonUtils.toJsonStr(resultWs); |
172 | 175 | ||
173 | sendMsgByWebsocket(resultJs, sid); | 176 | sendMsgByWebsocket(resultJs, sid); |
174 | } | 177 | } |
@@ -180,9 +183,9 @@ public class NmsController { | @@ -180,9 +183,9 @@ public class NmsController { | ||
180 | log.error("获取数据出错", e); | 183 | log.error("获取数据出错", e); |
181 | } | 184 | } |
182 | final String endTime = sdf.format(new Date()); | 185 | final String endTime = sdf.format(new Date()); |
183 | - System.out.println("出港信息,获取完毕,index的值为:" + index); | ||
184 | - System.out.println("结束时间:" + endTime); | ||
185 | - String resultJs = JSON.toJSONString(new ResultWs("获取数据,完成", resultList, "200", resultList.size(), resultList.size())); | 186 | + log.info("出港信息,获取完毕,index的值为:" + index); |
187 | + log.info("结束时间:" + endTime); | ||
188 | + String resultJs = GsonUtils.toJsonStr(new ResultWs("获取数据,完成", resultList, "200", resultList.size(), resultList.size())); | ||
186 | sendMsgByWebsocket(resultJs, sid); | 189 | sendMsgByWebsocket(resultJs, sid); |
187 | resultJson.setCode("200"); | 190 | resultJson.setCode("200"); |
188 | resultJson.setData(resultList); | 191 | resultJson.setData(resultList); |
@@ -323,4 +326,6 @@ public class NmsController { | @@ -323,4 +326,6 @@ public class NmsController { | ||
323 | e.printStackTrace(); | 326 | e.printStackTrace(); |
324 | } | 327 | } |
325 | } | 328 | } |
329 | + | ||
330 | + | ||
326 | } | 331 | } |
1 | +package com.sunyo.wlpt.cgonms.provide.utils; | ||
2 | + | ||
3 | +import com.fasterxml.jackson.databind.deser.std.DateDeserializers; | ||
4 | +import com.fasterxml.jackson.databind.ser.std.DateSerializer; | ||
5 | +import com.google.gson.Gson; | ||
6 | +import com.google.gson.GsonBuilder; | ||
7 | + | ||
8 | +import java.text.DateFormat; | ||
9 | + | ||
10 | +/** | ||
11 | + * @author 子诚 | ||
12 | + * Description: | ||
13 | + * 时间:2020/6/5 10:19 | ||
14 | + */ | ||
15 | +public class GsonUtils { | ||
16 | + | ||
17 | + private static Gson gson = null; | ||
18 | + | ||
19 | + /** | ||
20 | + * 判断gson对象是否存在了,不存在则创建对象 | ||
21 | + */ | ||
22 | + static { | ||
23 | + if (gson == null) { | ||
24 | + //gson = new Gson(); | ||
25 | + //当使用GsonBuilder方式时属性为空的时候输出来的json字符串是有键值key的,显示形式是"key":null,而直接new出来的就没有"key":null的 | ||
26 | + gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); | ||
27 | + } | ||
28 | + } | ||
29 | + | ||
30 | + /** | ||
31 | + * 无参的私有构造方法 | ||
32 | + */ | ||
33 | + private GsonUtils() { | ||
34 | + } | ||
35 | + | ||
36 | + /** | ||
37 | + * 封装一个 gson 将对象转换成json字符串的方法。 | ||
38 | + */ | ||
39 | + public static String toJsonStr(Object object) { | ||
40 | + String gsonString = null; | ||
41 | + if (gson != null) { | ||
42 | + gsonString = gson.toJson(object); | ||
43 | + } | ||
44 | + return gsonString; | ||
45 | + } | ||
46 | +} |
1 | package com.sunyo.wlpt.cgonms.provide.websocket; | 1 | package com.sunyo.wlpt.cgonms.provide.websocket; |
2 | 2 | ||
3 | 3 | ||
4 | -import cn.hutool.json.JSONObject; | ||
5 | -import cn.hutool.json.JSONUtil; | 4 | + |
6 | import cn.hutool.log.Log; | 5 | import cn.hutool.log.Log; |
7 | import cn.hutool.log.LogFactory; | 6 | import cn.hutool.log.LogFactory; |
8 | -import com.alibaba.fastjson.JSON; | ||
9 | import com.sunyo.wlpt.cgonms.provide.controller.NmsController; | 7 | import com.sunyo.wlpt.cgonms.provide.controller.NmsController; |
10 | -import com.sunyo.wlpt.cgonms.provide.response.ResultJson; | ||
11 | import com.sunyo.wlpt.cgonms.provide.response.ResultWs; | 8 | import com.sunyo.wlpt.cgonms.provide.response.ResultWs; |
9 | +import com.sunyo.wlpt.cgonms.provide.utils.GsonUtils; | ||
12 | import org.springframework.stereotype.Component; | 10 | import org.springframework.stereotype.Component; |
13 | 11 | ||
14 | import javax.annotation.Resource; | 12 | import javax.annotation.Resource; |
@@ -17,7 +15,7 @@ import javax.websocket.server.PathParam; | @@ -17,7 +15,7 @@ import javax.websocket.server.PathParam; | ||
17 | import javax.websocket.server.ServerEndpoint; | 15 | import javax.websocket.server.ServerEndpoint; |
18 | import java.io.IOException; | 16 | import java.io.IOException; |
19 | import java.util.concurrent.CopyOnWriteArraySet; | 17 | import java.util.concurrent.CopyOnWriteArraySet; |
20 | -import java.util.concurrent.atomic.AtomicInteger; | 18 | + |
21 | 19 | ||
22 | /** | 20 | /** |
23 | * Created by XYH on 2019/12/23. | 21 | * Created by XYH on 2019/12/23. |
@@ -54,7 +52,7 @@ public class WebSocketServer { | @@ -54,7 +52,7 @@ public class WebSocketServer { | ||
54 | log.info("有新窗口开始监听:" + sid + ",当前在线人数为" + getOnlineCount()); | 52 | log.info("有新窗口开始监听:" + sid + ",当前在线人数为" + getOnlineCount()); |
55 | this.sid = sid; | 53 | this.sid = sid; |
56 | try { | 54 | try { |
57 | - String resultStart = JSON.toJSONString(new ResultWs("连接成功,可以开始查询!" )); | 55 | + String resultStart = GsonUtils.toJsonStr(new ResultWs("连接成功,可以开始查询!")); |
58 | sendMessage(resultStart); | 56 | sendMessage(resultStart); |
59 | } catch (IOException e) { | 57 | } catch (IOException e) { |
60 | log.error("websocket IO异常"); | 58 | log.error("websocket IO异常"); |
-
请 注册 或 登录 后发表评论