【问题标题】:Using Restlet ClientResource cannot send entity to Restlet Service使用 Restlet ClientResource 无法将实体发送到 Restlet Service
【发布时间】: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 能够同时获取 idtype 字符串,但实体始终为 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&amp;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


【解决方案1】:

为了使该调用运行,请执行以下操作:

  • 将取自 GWT 版本的框架的 Json 扩展 (org.restlet.ext.json.jar) 添加到您的项目中
  • 将以下依赖项添加到您的 GWT 模块中

  • 然后更新您的代码如下:

    resource.post(new JsonRepresentation(MediaType.APPLICATION_JSON, jsonObject));

我注意到如果你发送一个表示而不是一个对象,它就会起作用。 主要问题是在处理对象时,核心模块没有如何序列化它。在 JVM 中,我们可以插入一个服务来发现自动将 bean 转换为表示的可用转换器,我们无法使用 GWT 实现这一点。

话虽如此,我认为您可以通过使用 bean 以更简单的方式集成您的客户端和服务器代码。 GWT 客户端代码可以使用特定的 GWT 序列化格式进行通信,任何其他客户端代码都可以使用 JSON。

看看这段代码:http://restlet.com/technical-resources/restlet-framework/guide/2.3/introduction/first-steps/first-application

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多