无法将 zookeeper 对象转换为 json,反之亦然

大家好,我有一个简单的问题,我似乎无法使用 jersey 中的 GSON 库将 zookeeper 对象转换为 json ,反之亦然。我得到的错误是


Exception in thread "main" java.lang.StackOverflowError

    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381)

    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376)

    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381)

    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376)

它一直在持续很大。从我搜索的内容来看,这似乎是嵌套太深的对象和递归的问题。这是我尝试的简单 POC


ZooKeeper zoo;

        try {

            zoo = new ZooKeeper("localhost:2182",5000,null);

            String obj=gson.toJson(zoo, ZooKeeper.class);

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

有人可以清楚地解释什么是实际问题,即使可以将 zookeeper 对象转换为 json 并将其使用(因为与之关联的所有线程)


呼啦一阵风
浏览 132回答 1
1回答

Helenr

ZooKeeper 是服务器而不是 DTO也许你想用 DTO 配置做一个 json我的提议public static void main(String[] args) {&nbsp; ZooKeeper zoo;&nbsp; try {&nbsp; &nbsp; ZooKeeperConfDTO conf = new ZooKeeperConfDTO("localhost:2182", 5000, null);&nbsp; &nbsp; zoo = runZoo(conf);&nbsp; &nbsp; String json = new Gson().toJson(conf);&nbsp; &nbsp; System.out.println(json); //---->{"connectString":"localhost:2182","sessionTimeout":5000}&nbsp; } catch (Exception e) {&nbsp; &nbsp; e.printStackTrace();&nbsp; }}private static ZooKeeper runZoo(ZooKeeperConfDTO conf) throws IOException {&nbsp; return new ZooKeeper(conf.connectString, conf.sessionTimeout, conf.watcher);}并创建了班级import org.apache.zookeeper.Watcher;public class ZooKeeperConfDTO {&nbsp; public String connectString;&nbsp; public int sessionTimeout;&nbsp; public Watcher watcher;&nbsp; public ZooKeeperConfDTO(String connectString, int sessionTimeout, Watcher watcher) {&nbsp; &nbsp; this.connectString = connectString;&nbsp; &nbsp; this.sessionTimeout = sessionTimeout;&nbsp; &nbsp; this.watcher = watcher;&nbsp; }}版本 2:为 ClientCnxn 创建你的 TypeAdapterimport java.io.IOException;import org.apache.zookeeper.ClientCnxn;import com.google.gson.TypeAdapter;import com.google.gson.stream.JsonReader;import com.google.gson.stream.JsonWriter;public class ClientCnxnAdapter extends TypeAdapter<ClientCnxn> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void write(JsonWriter writer, ClientCnxn cnxn) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; writer.beginObject();&nbsp; &nbsp; &nbsp; &nbsp; writer.name("sessionId");&nbsp; &nbsp; &nbsp; &nbsp; writer.value(cnxn.getSessionId());&nbsp; &nbsp; &nbsp; &nbsp; writer.name("timeOut");&nbsp; &nbsp; &nbsp; &nbsp; writer.value(cnxn.getSessionTimeout());&nbsp; &nbsp; &nbsp; &nbsp; writer.endObject();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public ClientCnxn read(JsonReader in) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }}并使用它public static void main(String[] args) {&nbsp; &nbsp; ZooKeeper zoo;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; zoo = new ZooKeeper("localhost:2182", 5000, null);&nbsp; &nbsp; &nbsp; &nbsp; Gson gson = new GsonBuilder().registerTypeAdapter(ClientCnxn.class, new ClientCnxnAdapter()).create() ;&nbsp; &nbsp; &nbsp; &nbsp; String json = gson.toJson(zoo);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(json); //---->{"cnxn":{"sessionId":0,"timeOut":0},"watchManager":{"dataWatches":{},"existWatches":{},"childWatches":{}}}&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java