I need to do a post request from a gwt app to a server. So far this works fine. However, originally I used an object that contained all the parameters send over to the server via a rpc request so I did not have to manage the serialization and deserialization myself. Now I send this stuff via a post request and on the server side I get something like username=blabla&location=blabla
I'd rather like to do something like this (pseudo code):
String serializedObject = parameterObject.serialize();
sendPostRequestWithContent(serializedObject);
and on the server side:
doPost(...)
String serializedObject = request.getContent();
ParameterObject parameterObject = ParameterObject.deserialize( serializedObject );
Any idea how I could do this?
There are different ways.
For simple objects manually serialize and deserialize (field1=123123&field2=1232)
Use JSON as payload.
For solution 2 you can use a JSON parser on the beackend (Jackson, Gson, etc) and on the client you can either manually serialize the object to JSON or one of these methods.
Related
I need to create a ReST service using Jersey 2.0. I need to send multiple documents and metadata to the client.
What is the best approach do to the achieve this.
I was able to send a MultiPart response from the server , but not sure how to read this from the client code
Let's say you have a document called "document1" which you want to get via your client.
In your REST-API your unique identifier for the document (the resource) could be:
http://example.com/restapi/documents/document1
As you want to READ data you do a HTTP-GET Request to that uri.
And here comes the important part for you: A resource can have multiple representations - meta data and binary data in your case.
So the client has to tell the server which representation type to get (content negotiation). This information can be set in the ACCEPT Header of the client request for instance.
You can use the content type "application/json" as a representation for the meta data.
Unfortunately you didn't tell us what kind of binary data you want to send.
If they are PDFs the content type would be "application/pdf" for instance. If the binary data doesn't have a specific type you can use "application/octet-stream".
Of course there is work to be done on the server side too. Here an example:
#Path("/documents/{documentname}")
public class docResource {
#GET #Produces("application/json")
public Response getDocumentMetaData(#PathParam("documentname") String docName) {
// Create a Response containing a json
}
#GET #Produces("application/pdf")
public Response getDocumentBinaryData(#PathParam("documentname") String docName) {
// Create a response containing the binary data
}
...
}
Jersey will check the accept header of the client and will run the appropriate method.
Also see: https://jersey.java.net/documentation/latest/jaxrs-resources.html
If you are using jersey with jackson you can also easily marshal a POJO to JSON and visa versa:
http://examples.javacodegeeks.com/enterprise-java/rest/jersey/json-example-with-jersey-jackson/
If you are not sure what to do in the "getDocumentBinaryData"-Method - checkout this simple example from mkyong:
http://www.mkyong.com/webservices/jax-rs/download-excel-file-from-jax-rs/
I'm looking for a better way to structure my rest client/rest api code. Currently, I have my rest api with routes like:
get /book
The literal string "/book" is defined on the server side, and on the client side, I have to use the same literal string exactly.
What I would like to do is, have a file serverside which has all of my routes in it, and send it to the client as json, which the client uses for routing.
The basic workflow would be:
Start the server
When the client connects, make a get request for the routes object
return an object like this:
{
getBook:"/book",
createBook:"/book/create"
}
that way my client can use the object returned for the routes instead of hard coding them.
Is this a good idea?
I'm not able to receive plain text using RestyGWT. A very simple test service looks like this:
#GET
#Consumes(MediaType.TEXT_PLAIN)
#Produces(MediaType.TEXT_PLAIN)
void test(MethodCallback<String> callback);
On the client side I always get:
Response was NOT a valid JSON document
Looking at the HTTP response I'm seeing that content type is set correctly:
Content-Type:text/plain
What's the problem? Why RestyGWT tries to parse this as JSON? Am I doing smth wrong?
If you will use TextCallback instead of MethodCallback<String> you won't get this error.
It will set Accept header automatically to text/plain, so you don't have to use #Produces adnotation in your async interface.
Well, RestyGWT is a Json library, AFAIK it is not made to handle plain text, or XML, or anything else. "foo" isn't a valid Json data, should be ["foo"] or {"stuff": "foo"}.
Indeed, in our Rest API, we tried to send pure text (an id), but we finally made a simple object wrapping the id.
I'm using GWT RPC Calls for Server Side Request so far and it's pretty good. I'm planning on separating my Code into Servlets and GWT Client Side. Since i'm using RPC calls, it seems impossible. The Reason i want to do like this is , i'm planning to provide white labeling option for my App. So if i could separate the code to client code and servlets, i can simply provide the White Labeled client code to my Partners to host on their server. I have checked with GWT RequestBuilder and Access-Control Allow-Origin : Origin from Client Header and it works fine.
However i need to implement gwt-serialization over RequestBuilder request and Servlet Responses. How can i do this ..?
Scenario I like to make:
RequestBuilder sending Serializable String(Which is a IsSerialiazible object) to Servlet.
Servlet deserializes the String to Java Object,Processes and Returns the String Response of a 'IsSerialiazable' Object.
The Response String recieved in GWT RequestBuilder deserialzes it back to a Java Object(JS after Compiling).
I have checked on RemoteServiceServlet class which seems to have some info on serializing and deserializing request and response. But i couldn't get it right to get it to work with RequestBuilder. Any ideas , Hope it will be helpful for everyone.
public final void processPost(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException,SerializationException
{
// Read the request fully.
//
String requestPayload = readContent(request);
// Let subclasses see the serialized request.
//
onBeforeRequestDeserialized(requestPayload);
// Invoke the core dispatching logic, which returns the serialized
// result.
//
String responsePayload = processCall(requestPayload);
// Let subclasses see the serialized response.
//
onAfterResponseSerialized(responsePayload);
// Write the response.
//
writeResponse(request, response, responsePayload);
}
GWT RPC and RequestBuilder serve different purposes. We cannot mix/match them.
GWT RPC - Services which fetch Data
GWT Request Builder - fire requests for static resources like js,css, json objects, querying soap services etc
The only feasible solution at the top of my mind for your approach of servicing requests is by using JSON - https://developers.google.com/web-toolkit/doc/latest/tutorial/JSON
You can keep your servlets code as is and then use RequestBuilder to query URL mapped to these servlets for JSON objects. Process the JSON objects using JSNI or Overlay concepts.
I'm trying to get the GWT Serialization & Deserialization source.Unfortunately, i have been stuck with other works and couldn't look on this right now. When i get the GWT Serialization Classes , i will update.
I've generated the web service client in eclipse for the OpenCalais WSDL using the "develop" client type. Actually I was following this post so not really going in detail. Now when I get the results this way: new CalaisLocator().getcalaisSoap().enlighten(key, content, requestParams);, I get the String object, containing the response XML. Sure it's possible to parse that XML, but I think there must be some way to do it automatically, e.g. getting the response object in the form of some list whatsoever?
The response from the SOAP interface is already parsed. The englighten() method returns an XML string. When you call it with SOAP, this response is wrapped within even more XML. The SOAP library already parses the outer SOAP XML and returns the result of the enlighten() method, which is also XML.