代码之家  ›  专栏  ›  技术社区  ›  Spiff

字符串数组上的SerializationException

  •  0
  • Spiff  · 技术社区  · 12 年前

    我缺少一些东西:

    在里面 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 回复  |  直到 12 年前
        1
  •  3
  •   David Levesque    12 年前

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

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

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

        2
  •  1
  •   11101101b    12 年前

    我在GWT代码中使用了字符串数组来进行客户端和服务器之间的通信。我认为您的问题在于您试图序列化HashMap。