簡體   English   中英

使用Restlet ClientResource無法將實體發送到Restlet服務

[英]Using Restlet ClientResource cannot send entity to Restlet Service

我在使用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 ,也可以獲取String 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>

為了使此調用運行,請執行以下操作:

  • 將取自GWT版本的框架的Json擴展(org.restlet.ext.json.jar)添加到您的項目中
  • 將以下依賴項添加到您的GWT模塊中

  • 然后更新您的代碼,如下所示:

    resource.post(新的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM