使用Gson,将javaBean转换成json格式

在pom文件中引入:

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>

使用举例:

                try {
                    System.out.println("存在失活的节点! 关键字: " + key);
                    WebSocketServer.sendInfo( new Gson().toJson(new WebSocketCommon(1,key,"下线通知")));
                } catch (IOException e) {
                    e.printStackTrace();
                }
发布了85 篇原创文章 · 获赞 21 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_41670928/article/details/103857559