String数组的SerializationException

时间:2012-09-24 12:20:12

标签: gwt gwt-rpc

我缺少一些东西:

Serializable Types中明确说明了序列化的内容:

  

类型是可序列化类型的数组(包括其他类型   可序列化数组)。

但我无法序列化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();

        }
    });
  }

另一方面,我可以将其序列化:

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

以下是我得到的例外情况:

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)

这是服务:

  @RemoteServiceRelativePath("greet")
    public interface GreetingService extends RemoteService
    {
        String greetServer(ArrayList name) throws IllegalArgumentException;
    }  

2 个答案:

答案 0 :(得分:3)

尝试使用泛型声明所有可序列化的集合。例如,GreetingService中的方法应为:

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

并使用类型化版本替换对ArrayList的所有其他引用。 GWT使用泛型声明来验证集合的内容是否可序列化,如果您不提供它们,它通常会出错。

答案 1 :(得分:1)

我在GWT代码中使用了String Arrays,用于客户端和服务器之间的通信。我相信你的问题在于你正在尝试序列化HashMap。