我从以下 thrift 对象生成了一个 java 对象:
struct Account {
1: required string accountType,
2: bool accountActive,
}
我编写了一个 java 代码,试图将 java 对象序列化为 json 字符串,然后将 json 字符串反序列化回 java 对象。我可以成功序列化但无法反序列化。
TSerializer serializer = new TSerializer(new TSimpleJSONProtocol.Factory());
TDeserializer deserializer = new TDeserializer(new TSimpleJSONProtocol.Factory());
Account a1 = new Account();
a1.setAccountType("P");
a1.setAccountActive(true);
String json = serializer.toString(a1);
System.out.println(json);
Account a2 = new Account();
deserializer.deserialize(a2, json, "UTF-8");
System.out.println(a2);
System.out.println(a2.getAccountType());
它不断抛出以下异常:
Exception in thread "main" org.apache.thrift.protocol.TProtocolException: Required field 'accountType' was not present! Struct: Account(accountType:null, accountActive:false)
谁能帮我弄清楚是什么问题?提前致谢!