PDA

View Full Version : retrieving payload from Ext.data.JsonWriter on the server



n3b
24 Sep 2010, 9:53 AM
I've been following the excellent RESTful store example http://dev.sencha.com/deploy/dev/examples/restful/restful.html and have got the incoming/reading part done.

I'm using a standard Java servlet to pick up the Put response when I try to perform an update. The Servlet's doPut method fires and in firebug I see a parameter called 'data' with a json string being sent.

(I am using encode : true on the JsonWriter)

However, when I inspect the parameter values in the servlet using 'request.getParameter("data")' I get nothing. Looking at the parameterNames enum it is completely empty.

I respect this is probably not an Ext issue but I was hoping someone could bang me on the head and point me in the right direction.

Cheers,
n

n3b
24 Sep 2010, 10:32 AM
After a bit of playing I got access to the request reader and assembled the data. For any other Java users here is how to get the JsonWriter payload



StringBuffer sb = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null){
sb.append(line);
}
} catch (Exception e) {
// TODO: handle exception
}
// access your json string in sb.toString()


Cheers,
n

bobringer
30 Nov 2010, 9:28 AM
n3b, why not just use a server side REST framework such as RESTEasy?

I am using RESTEasy using Jackson to consume/produce Ext friendly JSON.

You can do very simple stuff and just send up a JSON object that RESTEasy/Jackson automatically converts to a HashMap. This is extremely useful. You can do the same thing in your response... just return a Java HashMap and it automatically gets returned to the browser as a proper JSON object.


Getting more advanced... I have built my own classes to wrap the objects you are processing inside an "Ext JSON" object. Lots of introspection on your java objects gets you all the information you need to build very powerful responses that Ext consumes automatically. In this scenario, the server side of your REST API CRUD calls could be as simple as 2 very short lines of code.


Very useful stuff...