2009-08-29 7 views
5

In primo luogo, quello che volevo sapere è che quello che sto facendo è il modo giusto per farlo.Post di implementazione del restlet con json receive e response

Ho uno scenario in cui ho ricevuto una richiesta JSON e devo aggiornare il database con quello, una volta aggiornato il db, devo rispondere con il riconoscimento JSON.

quello che ho fatto finora è creare l'applicazione si estende classe come segue:

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

La mia classe di risorse estende la ServerResource e ho il seguente metodo nella mia classe di risorsa

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

La mia prima preoccupazione è che l'oggetto che ricevo nell'entità è inputrepresentation, quindi quando prendo jsonobject dalla jsonrepresentation creata ottengo sempre oggetto vuoto/nullo.

ho cercato di passare la richiesta JSON con il seguente codice così come il client collegato

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

client utilizzato per chiamare

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

Qualsiasi aiuto o indizi su questo è alta apprezzato?

Grazie, Rahul

+0

Considerate questa domanda sul Restlet-discutere forum ufficiale: http: // restl et.tigris.org/ds/viewForumSummary.do?dsForumId=4447 –

risposta

1

Quando uso il seguente JSON come la richiesta, funziona:

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

Nota le virgolette doppie e due punti supplementari che non sono nel vostro campione.

1

Utilizzando solo 1 JAR JSE-xyz/lib/org.restlet.jar, si potrebbe costruire JSON a mano sul lato client per semplici richieste:

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

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

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

Al lato server, utilizzando solo 2 vasetti - GSON-xyzjar e JSE-xyz/lib/org.restlet.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); 
    } 
} 
Problemi correlati