2009-08-29 14 views
5

İlk olarak, bilmek istediğim şey yapmak için doğru yoldur.Json alma kısıtlaması json alma ve yanıtla

Bir json isteği alacağım bir veritabanım var ve veritabanını güncellemeliyim, db güncellendikten sonra json onayına geri dönmem gerekiyor.

i bugüne kadar yaptığı aşağıdaki gibi sınıf uzanan uygulaması oluşturmak olduğunu var Ne:

 @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; 
    } 

My kaynak sınıfı ServerResource uzanan ve benim kaynak sınıfında aşağıdaki yöntemi var

@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); 
    } 

Benim ilk kaygım varlık aldım nesnesi giriş temsilciliği olduğunu jsonrepjecti jsonrepresment getirdiğinizde ben her zaman boş/null nesne olsun.

Ben yüksekliği takdir

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); 

Herhangi yardım veya bu konuda ipuçları derdik aşağıdaki kodla json isteği yanısıra

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); 
     } 
    }); 
}; 

Müşteri ekli istemci geçen denediniz mi?

sayesinde Rahul

+0

resmi Restlet-tartışmak forumunda bu soruyu soran düşünün // restl et.tigris.org/ds/viewForumSummary.do?dsForumId=4447 –

cevap

1

Ben istek olarak aşağıdaki JSON kullanmak , çalışır: senin numunede olmayan

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

Bildirim çift tırnak ve ek kolon.

1

sadece 1 JAR kullanma jse-xyz/lib/org.restlet.jar, basit istekleri için istemci tarafında elle JSON olustursaydiniz: Sadece kullanarak,

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

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

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

sunucu tarafında 2 kavanozları - GSON-xyzjar ve jse-xyz/lib/org.restlet.jar: http:

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); 
    } 
}