0

I want to serialize an object in GWT, which inherits other kinds of objects within this code. My result should be an System.out.print:

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream; 
import com.google.gwt.user.client.rpc.IsSerializable;  
import spa.shared.storages.RouteStorage;

public class RouteStorageHelper implements IsSerializable {

    public RouteStorageHelper() {

    }

    public void storeRoutes(RouteStorage routeStorage) {

        ByteArrayOutputStream baos;
        ObjectOutputStream out;
        baos = new ByteArrayOutputStream();

        try {
            out = new ObjectOutputStream(baos);
            out.writeObject(routeStorage);
            //byte[] byteArray=baos.toByteArray();
            out.close();
            byte[] serializedGraph=baos.toByteArray();
            System.out.println(serializedGraph);


        } catch (Exception sqle) {
            System.out.print(sqle);
        }
    }

But all i got, is a NotSerializableException, which tells, something is not serialized. I can not understand, because other subclass-objects also got the IsSerializable interface.

Other classes with this interface: RouteStorage, Storage, Patient, Employee, etc... Things i also use: String, Integer, HashMap, ArrayList, Date (java.util) and two enums Status and Qualifikation.

Is any of this Classes not serializable? Is there something different in GWT?

Gary Klasen
  • 1,001
  • 1
  • 12
  • 30

2 Answers2

1

Yes, here you use the java serialization which is based on java.io.Serializable interface. GWT interface IsSerializable doesn't extends java.io.Serializable, so from the java serializer point of view your objects are not serializables

1

On the client side for serialization GWT translates Java classes to JavaScript. To do so it has some restrictions on the classes you can use. Not all standard java classes are not supported, but a limited set. See more about this in the GWT documentation: Serializable Types.

The interface IsSerializable is an remnant of older GWT versions and you can just use the standard Serializable interface, which also makes something Java serializable on the server side.

Hilbrand Bouwkamp
  • 13,509
  • 1
  • 45
  • 52
  • So I do not have to do sth like this: public class `RouteStorageHelper implements IsSerializable, Serializable` but only use `Serializable`? – Gary Klasen Aug 01 '13 at 12:10
  • yes you only need `Serializable`. see: http://www.gwtproject.org/doc/latest/FAQ_Server.html#Does_the_GWT_RPC_system_support_the_use_of_java.io.Serializable – Hilbrand Bouwkamp Aug 01 '13 at 12:12