Hi Rahul,

I would need a complete application to test and explain why it isn't
working. However, I suggest that you simplify your resource this way:

        @Post("json")
        public JSONObject post(JSONObject jsonobject) throws
ResourceException {
                JSONObject json  = jsonobject.getJSONObject("request");
             setStatus(Status.SUCCESS_ACCEPTED);
                StringBuffer sb = new StringBuffer();
                ScanRequestAck ack = new ScanRequestAck();
                ack.statusURL = "http://localhost:8080/status/2713";;
                return ack.asJSON();

On the client-side, you could try something like:

        ClientResource requestResource = new
ClientResource("http://localhost:8080/thoughtclicksWeb/request";);
        JSONObject reply = requestResource.post(new JSONObject(jsonstring),
JSONObject.class);

Hope this helps!

Best regards,
Jerome Louvel
--
Restlet ~ Founder and Lead developer ~ http://www.restlet.org
Noelios Technologies ~ Co-founder ~ http://www.noelios.com


-----Message d'origine-----
De : Rahul Juneja [mailto:rjun...@verisign.com] 
Envoyé : samedi 29 août 2009 20:51
À : discuss@restlet.tigris.org
Cc : rahul.jun...@gmail.com
Objet : Restlet 2 Snapshot for POST with JSON

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

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=23885
58

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2402810

Reply via email to