まず、私が知りたかったのは、私がやっていることは正しい方法だということです。
私は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);
これに関するヘルプや手がかりは高く評価されていますか?
ありがとう、ラフル