Sat*_*hya 5 gwt google-app-engine json google-cloud-endpoints resty-gwt
我正在为我的休息服务使用 Google 云端点。我正在使用 RestyGWT 在 GWT Web 客户端中使用这些数据。
我注意到,当我尝试将 JSON 转换为 POJO 时,云端点会自动将长数据类型括在双引号中,这导致 RestyGWT 中出现异常。
这是我的示例代码。
@Api(name = "test")
public class EndpointAPI {
@ApiMethod(httpMethod = HttpMethod.GET, path = "test")
public Container test() {
Container container = new Container();
container.testLong = (long)3234345;
container.testDate = new Date();
container.testString = "sathya";
container.testDouble = 123.98;
container.testInt = 123;
return container;
}
public class Container {
public long testLong;
public Date testDate;
public String testString;
public double testDouble;
public int testInt;
}
Run Code Online (Sandbox Code Playgroud)
}
这是云端点以 JSON 形式返回的内容。您可以看到 testLong 被序列化为“3234345”而不是 3234345。

我有以下问题。(1) 如何删除长值中的双引号?(2) 如何将字符串格式更改为 "yyyy-MMM-dd hh:mm:ss" ?
问候, 萨蒂亚
您使用什么版本的restyGWT?你尝试过1.4快照吗?我认为这是负责解析 Restygwt 中的 long 的代码(1.4),它可能对您有帮助:
public static final AbstractJsonEncoderDecoder<Long> LONG = new AbstractJsonEncoderDecoder<Long>() {
public Long decode(JSONValue value) throws DecodingException {
if (value == null || value.isNull() != null) {
return null;
}
return (long) toDouble(value);
}
public JSONValue encode(Long value) throws EncodingException {
return (value == null) ? getNullType() : new JSONNumber(value);
}
};
static public double toDouble(JSONValue value) {
JSONNumber number = value.isNumber();
if (number == null) {
JSONString val = value.isString();
if (val != null){
try {
return Double.parseDouble(val.stringValue());
}
catch(NumberFormatException e){
// just through exception below
}
}
throw new DecodingException("Expected a json number, but was given: " + value);
}
return number.doubleValue();
}
Run Code Online (Sandbox Code Playgroud)