【发布时间】:2015-02-28 13:45:30
【问题描述】:
我在使用 Restlet 客户端调用工作休息服务时遇到问题:
final ClientResource resource = new ClientResource(Routes.PUBLIC_STORE_API);
resource.setOnResponse(new Uniform() {
public void handle(Request request, Response response) {
try {
Status status = response.getStatus();
if(!Status.isError(status.getCode())){
String jsonResponse = response.getEntity().getText();
} else {
// Handle error
}
} catch (Exception e){
callback.failure(new Throwable(e.getMessage()));
}
}
});
JsniHelper.consoleLog("Adding object id=" + id + " type=" + type + " data=" + jsonObject);
resource.getReference().addQueryParameter("type", type);
resource.getReference().addQueryParameter("id",id);
resource.post(jsonObject, MediaType.APPLICATION_JSON);
对象上方的日志点是一个有效的 JSON 字符串。
Restlet ServerResource 能够同时获取 id 和 type 字符串,但实体始终为 null:
@Post("json")
public Representation add(Representation entity){ // omitted }
我尝试使用 CURL,Rest 服务能够正确处理 JSON 字符串。
我在 GWT 中的代码可能有什么问题?
更新:我在 POM 中有这个
<dependency>
<groupId>org.restlet.gae</groupId>
<artifactId>org.restlet.ext.gwt</artifactId>
<version>2.2-RC3</version>
</dependency>
【问题讨论】:
-
你能分享一下你用的
curl命令,这样我们可以看到它和上面列出的java匹配吗? -
我在这里做了另一个测试:
curl -H "Content-Type: application/json" -d '{"username":"xyz","password":"xyz"}' http://localhost:8888/publicstore?id=null&type=user -
上面那个工作并且能够保存到数据存储中,来自 GWT(使用 Restlet GWT)的 post 调用总是失败。但是,我有另一个调用 get 调用类似的代码,并且工作正常。此外查询参数没问题,服务器得到它,只有表示实体为空。
-
你在服务端安装了Restlet Framework的GWT扩展吗?需要与 Restlet/GWT 客户端代码通信,不依赖 JSON。
-
@JeromeLouvel 是的,我在 POM 中有这个:org.restlet.ext.gwt
标签: java google-app-engine rest gwt restlet