Hello Rahul,

I send you a simple test case where a client send a Json string to a 
resource. It works properly with the current Restlet trunk.
It juste require the presence of the org.restlet, org.restlet.ext.json 
and org.json jars.

Could you precise the version of the Reslet you are using?

best regards,
Thierry Boileau

> I am trying very simple scenario in which my resource method accepts 
> Representation and then in the method I pass that representation to 
> jsonrepresentation code is as follows:
>  
>
> public Representation scanRequest(Representation entity){
>
>             *//Entity recieved is InputRepresentation*
>
>             JsonRepresentation rep = new 
> JsonRepresentation(entity);          
>
>             rep.getJsonObject();*//This returns null as jsonobject is 
> null in the rep formed above.*
>
> }
>
>  
>
>  And the content type of the message I am sending is application/json. 
> Also tried with text/json but no luck.
>
>  
>
> Any clues what can be wrong with this.
>
>  
>
> Thanks,
>
> Rahul
>

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2393559
package json;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;

public class HelloWorldResource extends ServerResource {

    /**
     * Handle post request.
     * 
     * @param entity
     * @return
     * @throws IOException
     */
    @Post
    public String Representation(Representation entity) throws IOException {
        JsonRepresentation rep = new JsonRepresentation(entity);
        try {
            JSONObject jo = rep.getJsonObject();
            return jo.get("name").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return "error";
    }

}
package json;

import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.data.Protocol;
import org.restlet.resource.ClientResource;
import org.restlet.routing.Router;

public class MyApplication extends Application {
    /**
     * Creates a root Restlet that will receive all incoming calls.
     */

    @Override
    public Restlet createRoot() {
        // Create a router Restlet that routes each call to a
        // new instance of HelloWorldResource.
        Router router = new Router(getContext());
        router.attachDefault(HelloWorldResource.class);
        return router;
    }

    public static void main(String[] args) throws Exception {
        Component component = new Component();
        component.getServers().add(Protocol.HTTP, 8182);

        component.getDefaultHost().attach(new MyApplication());
        component.start();
        ClientResource res = new ClientResource("http://localhost:8182/";);
        res.post("{name:value}").write(System.out);
        component.stop();
    }

}

Reply via email to