6

まず、私が知りたかったのは、私がやっていることは正しい方法だということです。

私はjsonリクエストを受け取り、それでデータベースを更新しなければならないシナリオがあります。データベースが更新されたら、json確認応答で応答する必要があります。

私がこれまでに行ったことは、次のようにアプリケーションを拡張するクラスを作成することです:

     @Override  
     public Restlet createRoot() {  
         // Create a router Restlet that routes each call to a  
         // new instance of ScanRequestResource.  
         Router router = new Router(getContext());  

         // Defines only one route  
         router.attach("/request", RequestResource.class);  

         return router;  
     }  

私のリソース クラスは ServerResource を拡張しており、私のリソース クラスには次のメソッドがあります。

@Post("json")
public Representation post() throws ResourceException {
    try {
        Representation entity = getRequestEntity();
        JsonRepresentation represent = new JsonRepresentation(entity);
        JSONObject jsonobject = represent.toJsonObject();
        JSONObject json  = jsonobject.getJSONObject("request");

        getResponse().setStatus(Status.SUCCESS_ACCEPTED);
        StringBuffer sb = new StringBuffer();
        ScanRequestAck ack = new ScanRequestAck();
        ack.statusURL = "http://localhost:8080/status/2713";
        Representation rep = new JsonRepresentation(ack.asJSON());

        return rep;

    } catch (Exception e) {
        getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
    }

私の最初の懸念は、エンティティで受け取るオブジェクトがinputrepresentationであるため、作成されたjsonrepresentationからjsonobjectをフェッチすると、常に空/nullオブジェクトを取得することです。

次のコードとクライアントが接続されたjsonリクエストを渡そうとしました

function submitjson(){
alert("Alert 1");
    $.ajax({
        type: "POST",
        url: "http://localhost:8080/thoughtclicksWeb/request", 
        contentType: "application/json; charset=utf-8",
        data: "{request{id:1, request-url:http://thoughtclicks.com/status}}",
        dataType: "json",
        success: function(msg){
            //alert("testing alert");
            alert(msg);
        }
  });
};

呼び出しに使用されるクライアント

    ClientResource requestResource = new ClientResource("http://localhost:8080/thoughtclicksWeb/request");
        Representation rep = new JsonRepresentation(new JSONObject(jsonstring));
    rep.setMediaType(MediaType.APPLICATION_JSON);
    Representation reply = requestResource.post(rep);

これに関するヘルプや手がかりは高く評価されていますか?

ありがとう、ラフル

4

2 に答える 2

2

JAR jse-xyz/lib/org.restlet.jarを 1 つだけ使用して、クライアント側で手動で JSON を作成し、単純な要求を行うことができます。

ClientResource res = new ClientResource("http://localhost:9191/something/other");

StringRepresentation s = new StringRepresentation("" +
    "{\n" +
    "\t\"name\" : \"bank1\"\n" +
    "}");

res.post(s).write(System.out);

サーバー側では、gson-xyzjarjse-xyz/lib/org.restlet.jarの 2 つの JAR のみを使用します。

public class BankResource extends ServerResource {
    @Get("json")
    public String listBanks() {
        JsonArray banksArray = new JsonArray();
        for (String s : names) {
            banksArray.add(new JsonPrimitive(s));
        }

        JsonObject j = new JsonObject();
        j.add("banks", banksArray);

        return j.toString();
    }

    @Post
    public Representation createBank(Representation r) throws IOException {
        String s = r.getText();
        JsonObject j = new JsonParser().parse(s).getAsJsonObject();
        JsonElement name = j.get("name");
        .. (more) .. .. 

        //Send list on creation.
        return new StringRepresentation(listBanks(), MediaType.TEXT_PLAIN);
    }
}
于 2013-05-29T01:09:22.880 に答える
1

次のJSONをリクエストとして使用すると、機能します。

{"request": {"id": "1", "request-url": "http://thoughtclicks.com/status"}}

サンプルに含まれていない二重引用符と追加のコロンに注意してください。

于 2009-09-09T15:28:00.743 に答える