开发者

Serialize GWT value object and deserialize it at server side

开发者 https://www.devze.com 2023-03-09 16:14 出处:网络
I have a GWT application that needs to support the downloading of large .csv files. This is done with a ReportServlet which sets the proper content-type and other headers needed for the browser to ope

I have a GWT application that needs to support the downloading of large .csv files. This is done with a ReportServlet which sets the proper content-type and other headers needed for the browser to open the download dialog and start the spreadsheet application.

Since the reports have several filter parameters, I need to pass them to the server somehow. I can not use AJAX callbacks because in that case the browser does not pop up the download dialog. Currently I am doing this by creating a form with hidden fields for each parameter and parse them each at the serv开发者_运维知识库er side by hand.

It would be a much more elegant solution to have a serializer for the value object, put the serialized value in one form parameter, deserialize on the server and use it as we were in a regular RPC call.

At first I tried to search the GWT codebase for some serializer/deserializer api but I could not find any. After that I searched for JSON serializers but non of them had either a simple API or support for GWT. These two JSON serializers: http://code.google.com/p/gwtprojsonserializer/ and http://sites.google.com/site/gson/ could be great, but gwtprojson serializes Dates as timestamps and put the classname in the serialized string while gson as a dateformat string and no classname in the serialized string.

So to finally get to the point

Could anyone suggest an object serializer/deserializer that works both in GWT and java? Not neccessarily JSON, any other format will do.

or

Has anyone experience with using the serializer libraries mentioned above together, or with other libs that can serialize in GWT and deserialize in Java?

Thanks in advance! pentike


You can simply produce JSON in GWT via OverlayTypes: Json <-> Java serialization that works with GWT

OTOH, how about doing it in two steps:

  1. Use RPC to send arbitrary filter objects to server. Server saves this object (database/memcache/session depends on your needs) and returns a unique filter ID that identifies this filter object.

  2. Make a file-download request with a fileter ID as a sole parameter. Servers looks up the fileter object, prepares the CSV and sends it back.

The upside of this is:

  • No serialization/deserialization pain.
  • In case CSV generation takes a long time, you could generate CSV when initial RPC is made and start the download when RPC finishes. In the mean time you could show a nice dialog/notification.
  • Filter IDs could be one-time/time-limited/IP-locked, so users can't record URL and send it to someone else.


I have written a custom deserializer for GSon. It is quite simple, so it works well with gwtprojsonserializer.

    public class MyDateAdapter implements JsonDeserializer {

  /*
   * (non-Javadoc)
   * 
   * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement,
   * java.lang.reflect.Type, com.google.gson.JsonDeserializationContext)
   */
  @Override
  public Date deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext ctx)
                                                                                             throws JsonParseException {
    return new Date(jsonElement.getAsJsonPrimitive().getAsLong());
  }

}

We need to register our custom deserializer:

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Date.class, new MyDateAdapter());
    Gson gson = gsonBuilder.create();
0

精彩评论

暂无评论...
验证码 换一张
取 消