0

There is something I'm missing:

In Serializable Types it's explicitely stated what's serialized:

The type is an array of serializable types (including other serializable arrays).

However I can't serialize new String[]{"s"}

public void onModuleLoad()
{

    HashMap<String, String[]> p = new HashMap<String, String[]>();
    p.put("xxx", new String[]{"s"});

    ArrayList params = new ArrayList();
    params.add(p);


    greetingService.greetServer(params, new AsyncCallback<String>()
    {

        @Override
        public void onSuccess(String result)
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void onFailure(Throwable caught)
        {
            caught.printStackTrace();

        }
    });
  }

On the other hand I can serialize this:

    ArrayList list = new ArrayList();
    list.add("s");

Here's the exception I get:

com.google.gwt.user.client.rpc.SerializationException
    at com.google.gwt.user.client.rpc.impl.SerializerBase.getTypeHandler(SerializerBase.java:153)
    at com.google.gwt.user.client.rpc.impl.SerializerBase.serialize(SerializerBase.java:125)
    at com.google.gwt.user.client.rpc.impl.ClientSerializationStreamWriter.serialize(ClientSerializationStreamWriter.java:183)
    at com.google.gwt.user.client.rpc.impl.AbstractSerializationStreamWriter.writeObject(AbstractSerializationStreamWriter.java:126)
    at com.google.gwt.user.client.rpc.core.java.util.Map_CustomFieldSerializerBase.serialize(Map_CustomFieldSerializerBase.java:53)
    at com.google.gwt.user.client.rpc.core.java.util.HashMap_CustomFieldSerializer.serialize(HashMap_CustomFieldSerializer.java:39)
    at com.google.gwt.user.client.rpc.core.java.util.HashMap_FieldSerializer.serial(HashMap_FieldSerializer.java:23)
    at com.google.gwt.user.client.rpc.impl.SerializerBase.serialize(SerializerBase.java:126)
    at com.google.gwt.user.client.rpc.impl.ClientSerializationStreamWriter.serialize(ClientSerializationStreamWriter.java:183)
    at com.google.gwt.user.client.rpc.impl.AbstractSerializationStreamWriter.writeObject(AbstractSerializationStreamWriter.java:126)
    at com.google.gwt.user.client.rpc.core.java.util.Collection_CustomFieldSerializerBase.serialize(Collection_CustomFieldSerializerBase.java:44)
    at com.google.gwt.user.client.rpc.core.java.util.ArrayList_CustomFieldSerializer.serialize(ArrayList_CustomFieldSerializer.java:39)
    at com.google.gwt.user.client.rpc.core.java.util.ArrayList_FieldSerializer.serial(ArrayList_FieldSerializer.java:23)
    at com.google.gwt.user.client.rpc.impl.SerializerBase.serialize(SerializerBase.java:126)
    at com.google.gwt.user.client.rpc.impl.ClientSerializationStreamWriter.serialize(ClientSerializationStreamWriter.java:183)
    at com.google.gwt.user.client.rpc.impl.AbstractSerializationStreamWriter.writeObject(AbstractSerializationStreamWriter.java:126)
    at com.example.client.GreetingService_Proxy.greetServer(GreetingService_Proxy.java:31)
    at com.example.client.AnotherTest.onModuleLoad(AnotherTest.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:396)
    at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:200)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:525)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363)
    at java.lang.Thread.run(Thread.java:662)

Here's the service:

  @RemoteServiceRelativePath("greet")
    public interface GreetingService extends RemoteService
    {
        String greetServer(ArrayList name) throws IllegalArgumentException;
    }  
4
  • 1
    Can you include the exception? I'm in the opinion that sending a HashMap is ok (sending a Map would be ok too, but in case of GWT-RPC they even recommend concrete classes, to generate serialization code only for what's needed) Commented Sep 24, 2012 at 13:42
  • Just for testing, you could try serializing HashMap<String,SomeContainer> (being SomeContainer a class with the array inside). Or, HashMap<String,List<String>>. I know it's not what docs says... but it could give some light if it works... Commented Sep 24, 2012 at 13:45
  • I posted the exception. I'll try what you suggest Commented Sep 24, 2012 at 13:59
  • An array of HashMaps that each contain a number of arrays is a bad idea even if it works. Simplify the data structure. Commented Sep 24, 2012 at 14:59

2 Answers 2

3

Try declaring all your serializable collections with generics. For example, the method in GreetingService should be:

String greetServer(ArrayList<HashMap<String, String[]>> name) throws IllegalArgumentException;

and also replace all other references to ArrayList with the typed version. GWT uses the generics declaration to validate that the content of collections is serializable and it will often give errors if you don't provide them.

Sign up to request clarification or add additional context in comments.

5 Comments

For communication I use a data structure that I don't have control over it. It was created for a swing application. For the GWT application I convert all the stuff that can't be serialized to ArrayLists and HashMaps and that works, but I noticed that with String[] it's not always working. I would expect either that something is serializable or that it's not according to the documentation.
@DavidLevesque is right. You should only use serializable types. An ArrayList is an ÀrrayList<?>`. Not serializable. If you cannot control server types convert them. You surely have to implement the GWTService layer (implementation of the service interface on server). So you can use generics. If you need to copy array and pass item one by one, do it.
@Helios: "An ArrayList is an ÀrrayList<?>. Not serializable". Well I do serialize HashMaps and ArrayList and they don't use generics. It works. I've read that's not good practice not to use generics. Is there a link that states HashMaps/ArrayLists without generics can't be serialized? If it's the standard documentation I' find it. I agree that I must create this GWTService layer anyway. It does exist actually but it converts only what's absolutely necessary.
@user905374: no, just guessed that it couldn't be because GWT should have to generate serialization for each item, and being "?" it should complain about serializing Object. But I don't know for sure.
@user905374: one more thing. There are some things that work in DevMode because (I think) it doesn't have to convert to JavaScript a lot of stuff. It can use then all the JRE classes.
1

I have used String Arrays in my GWT code for communication between client and server. I believe you're problem is that you're trying to serialize the HashMap.

1 Comment

I have never used GWT-RPC. I use GWT front-end interfaced with Spring REST services or RequestFactory back-end. When converting between a Java object and a JavaScript object, you can't use any data structure you want. A Java Array (e.g. String[]) is converted to a JavaScript Array (e.g. JsArray<String>) for use on the client. I would use an array of String arrays, or create your own serializable object that contains the data you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.