Restlet implementing post with json receive and response

Rahul Juneja picture Rahul Juneja · Aug 29, 2009 · Viewed 17.4k times · Source

First, what i wanted to know is what i am doing is the right way to do it.

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.

What i have done so far is create the class extending application as follows:

     @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 resource class is extending the ServerResource and i have the following method in my resource class

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

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.

I have tried passing the json request with the following code as well as the client attached

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 used to call

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

Any help or clues on this is hight appreciated ?

Thanks, Rahul

Answer

Ashwin Jayaprakash picture Ashwin Jayaprakash · May 29, 2013

Using just 1 JAR jse-x.y.z/lib/org.restlet.jar, you could construct JSON by hand at the client side for simple requests:

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

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

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

At the server side, using just 2 JARs - gson-x.y.z.jar and jse-x.y.z/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);
    }
}