Note that there are some explanatory texts on larger screens.

plurals
  1. PORestlet implementing post with json receive and response
    primarykey
    data
    text
    <p>First, what i wanted to know is what i am doing is the right way to do it.</p> <p>I have a scenario where i have will receive a json request and i have to update the database with that, once the db is updated i have to respond back with the json acknowledgment.</p> <p>What i have done so far is create the class extending application as follows:</p> <pre><code> @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; } </code></pre> <p>My resource class is extending the ServerResource and i have the following method in my resource class</p> <pre><code>@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); } </code></pre> <p>My first concern is the object i receive in the entity is inputrepresentation so when i fetch the jsonobject from the jsonrepresentation created i always get empty/null object.</p> <p>I have tried passing the json request with the following code as well as the client attached</p> <pre><code>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); } }); }; </code></pre> <p>Client used to call</p> <pre><code> 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); </code></pre> <p>Any help or clues on this is hight appreciated ?</p> <p>Thanks, Rahul</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload