亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法將 zookeeper 對象轉換為 json,反之亦然

無法將 zookeeper 對象轉換為 json,反之亦然

呼啦一陣風 2022-07-14 09:53:35
大家好,我有一個簡單的問題,我似乎無法使用 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)它一直在持續很大。從我搜索的內容來看,這似乎是嵌套太深的對象和遞歸的問題。這是我嘗試的簡單 POCZooKeeper 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 并將其使用(因為與之關聯的所有線程)
查看完整描述

1 回答

?
Helenr

TA貢獻1780條經驗 獲得超4個贊

ZooKeeper 是服務器而不是 DTO


也許你想用 DTO 配置做一個 json


我的提議


public static void main(String[] args) {

  ZooKeeper zoo;

  try {

    ZooKeeperConfDTO conf = new ZooKeeperConfDTO("localhost:2182", 5000, null);

    zoo = runZoo(conf);

    String json = new Gson().toJson(conf);

    System.out.println(json); //---->{"connectString":"localhost:2182","sessionTimeout":5000}

  } catch (Exception e) {

    e.printStackTrace();

  }

}


private static ZooKeeper runZoo(ZooKeeperConfDTO conf) throws IOException {

  return new ZooKeeper(conf.connectString, conf.sessionTimeout, conf.watcher);

}

并創建了班級


import org.apache.zookeeper.Watcher;


public class ZooKeeperConfDTO {

  public String connectString;

  public int sessionTimeout;

  public Watcher watcher;


  public ZooKeeperConfDTO(String connectString, int sessionTimeout, Watcher watcher) {

    this.connectString = connectString;

    this.sessionTimeout = sessionTimeout;

    this.watcher = watcher;

  }

}

版本 2:


為 ClientCnxn 創建你的 TypeAdapter


import 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> {

    @Override

    public void write(JsonWriter writer, ClientCnxn cnxn) throws IOException {

        writer.beginObject();

        writer.name("sessionId");

        writer.value(cnxn.getSessionId());

        writer.name("timeOut");

        writer.value(cnxn.getSessionTimeout());

        writer.endObject();

    }


    @Override

    public ClientCnxn read(JsonReader in) throws IOException {

        return null;

    }

}

并使用它


public static void main(String[] args) {

    ZooKeeper zoo;

    try {

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

        Gson gson = new GsonBuilder().registerTypeAdapter(ClientCnxn.class, new ClientCnxnAdapter()).create() ;

        String json = gson.toJson(zoo);

        System.out.println(json); //---->{"cnxn":{"sessionId":0,"timeOut":0},"watchManager":{"dataWatches":{},"existWatches":{},"childWatches":{}}}

    } catch (Exception e) {

        e.printStackTrace();

    }

}


查看完整回答
反對 回復 2022-07-14
  • 1 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號