1

Most of my application uses GWT-RPC to communicate between the servet and client. However a few classes need to build a get request manually by "string smashing" the URL with various params like this...

public static String getTemplateImage(String templateInstanceId, PageImage pageImage) {
    return GWT.getHostPageBaseURL() + TEMPLATE_INSTANCE_IMAGE_SERVLET_PATH
            + "?templateInstanceId=" + templateInstanceId
            + "&fileName=" + pageImage.getImageId()
            + "&isBackground=false"
            + "&cropX=" + pageImage.getCropX()
            + "&cropY=" + pageImage.getCropY()
            + "&cropWidth=" + pageImage.getCropWidth()
            + "&cropHeight=" + pageImage.getCropHeight();
}

I was wondering, is there a way to piggyback GWT's rather awesome GWT-RPC object marshaller to do this?

public static String getTemplateImage(String templateInstanceId, PageImage pageImage) {
    return GWT.getHostPageBaseURL() + TEMPLATE_INSTANCE_IMAGE_SERVLET_PATH
            + "?templateInstanceId=" + templateInstanceId
        + "&pageImage=" + SomeGWTClass.toRpcString(pageImage)
       ;
}

Then on the servlet side do this...

 @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        final String templateInstanceId = req.getParameter("templateInstanceId");
        final String strPageImage= req.getParameter("pageImage");
        **final PageImage pageImage = SomeGWTClass.unmarshallString(PageImage.class,strPageImage);**    
}
benstpierre
  • 32,833
  • 51
  • 177
  • 288

2 Answers2

0

I am not whether the GWT RPC would be a good idea to use this way. GWT Serialization classes located in com.google.gwt.user.client.rpc and com.google.gwt.server.rpc packages are GWT's internal classes and you are not supposed to call it from your application code (it's ok if you are writing a general purpose framework and you are willing to tweak it when new version of GWT ship, possibly with breaking changes in the API of these classes).

The reason this kind of generic functionality is hard to come by in GWT is that it lacks Java's reflection support. You can drop into JavaScript, though, to get back some of this flexibility. For example, if you can turn your PageImage class into a GWT Overlay type, you can use new JSONObject(pageImage).toString() to marshal the object into json. A JSON library like XStream can turn this string back into a Java object on the server.

See this question for the details about GWT Overlay type technique.

Community
  • 1
  • 1
Tahir Akhtar
  • 11,385
  • 7
  • 42
  • 69
0

Basically it is possible to do something like this, but it is going to be quite difficult to implement. Server part is quite easy, the problem will be on client side.

Since GWT lacks reflection, it is generating at compile time classes like FieldSerializer. For any class passed via GWt-RPC service GWT will create a FieldSerializer, which knows how to serialize/deserialize the object.

So in order to use this functionality, you will need to create some kind of framework, where you will indicate that you want to serialize/deserialize some object, define you custom generator, which will generate all the stuff you need and etc.

But there is also an easy way (but it's very dirty hack, I don't really recommend to use it but who cares=). Add classes you want to serialize to some GWT-RPC service, than run the compiler with -gen option. It will write all those FieldSerializer to the disk. Than you can simply include them in your application and use them. But you will need to regenerate them every time you change the serialized class. It is quite a hack, but we used it few times for debugging purposes.

So the choice is yours. I personally would recommend to forget about GWT-RPC mecahnism, and use overlay-type + JSON parse/stringify

jusio
  • 9,850
  • 1
  • 42
  • 57