无法将 zookeeper 对象转换为 json,反之亦然
cannot convert a zookeeper object to json and vice versa
大家好我有一个简单的问题,我似乎无法将 zookeeper 对象转换为 json,反之亦然使用 jersey for java web services.The 中的 GSON 库出现错误是
Exception in thread "main" java.lang.WhosebugError
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)
而且它继续发展得相当大。根据我的搜索,这个 semms 是对象嵌套太深和递归的问题。这是我尝试的简单 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(因为所有线程都与之关联)
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);
}
并创建了 class
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();
}
}
大家好我有一个简单的问题,我似乎无法将 zookeeper 对象转换为 json,反之亦然使用 jersey for java web services.The 中的 GSON 库出现错误是
Exception in thread "main" java.lang.WhosebugError
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)
而且它继续发展得相当大。根据我的搜索,这个 semms 是对象嵌套太深和递归的问题。这是我尝试的简单 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(因为所有线程都与之关联)
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);
}
并创建了 class
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();
}
}