Problema de serialización de GWT al hacer RPC

i am learning GWT.

I got a error regarding the serilizablity.

breif discription of my problem

In class Customproperties

package com.exp.shared;


import java.io.Serializable;
import java.util.List;

public class Customproperties implements Serializable  {

    private Object value;
    private List<?> values;
          // more variable

    public Customproperties() {
        // TODO Auto-generated constructor stub
    }

    public Customproperties(String propertyName, List<?>  object,
            String propertyType, boolean mulitiValued, String cardinality, Boolean required) {

        this.propertyName=propertyName;
        this.values=object;
                // more initialization
    }

    public Customproperties(String propertyName,  List<?> object, String propertyType,
            boolean multiValued) {
        this.propertyName=propertyName;
        this.values=object;
                // more initialization
    }

    public Object getValue() {
              return value;
    }
    public List<?> getValues() {
        return values;
    }
 }

In server package in one of the classImpl i am using a object of Customproperties

      if (doc.getPropertyValue("cmis:objectTypeId").toString().equals("cmis:document")) {

    customproperty=new Customproperties(p.getDefinition().getDisplayName(), p.getValue(), p.getType().toString(),p.isMultiValued());
       }
else {
    customproperty=new Customproperties(p.getDefinition().getDisplayName(), p.getValues(), p.getType().toString(),p.isMultiValued(),value.getCardinality(),value.isRequired());
            }

here in if condntion p.getValue() returns Object. and in else condition p.getValues() returns List.

in CustomProperties class cuando cambio el object variable a string it is working perfectly fine.

but i dont change it it gives me error.

com.exp.shared.Customproperties' was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.: instance = com.exp.shared.Customproperties@1aa5344
        at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serialize(ServerSerializationStreamWriter.java:619)

I dont want to change it String . i just want to receive Object. cause this object can be some times String, date, int.

Plzz help.

preguntado el 22 de mayo de 12 a las 17:05

3 Respuestas

You should read the GWT docs about RPC serialization aquí.

The class java.lang.Object is not serializable, therefore you cannot expect that a collection of Object types will be serialized across the wire.

This is why you get your exception. In the code you've given, you're not using the field value. Both constructors on your class only set the values list. So if you're not using the field value, just remove it and it will work. But assuming that's a mistake and you do need to use it ...

You will have to know all the different possible types your value can have. And then either you have different fields, like intValue, dateValue, stringValue ... Or you can have one String field, and serialize your objects into strings like this.

public class CustomProperties {
    private String value;
    private String type;

    private void setValue(Object value, String type) {
        if (value != null) {
            this.value = value.toString();
            this.type = type;
        }
    }

    private Object getValue() {
        if (value != null) {
            if ("int".equals(type)) return Integer.valueOf(value);
            elseif ("date".equals(type)) return // Parse date from value here
            elseif ("string".equals(type)) return (String) value;
            // other cases
        }
        return value;
    }

}

contestado el 22 de mayo de 12 a las 17:05

In GWT when declaring containers never declare generic types like List.
Always use the more specific types like ArrayList.

contestado el 22 de mayo de 12 a las 17:05

: thanks for the suggestion i will do that. but that is not my problem. I am passing a object in constructor coz of wich i am gettin serializablity error. how to remove that without changing it to String or any other data type. - Aprendiz de código nuevo

on server side of gwt you can use whatever type you need gwt has no issues with it. but on client side you can check the possible types by instanceof keyward and evergthing will be fine. - Cabeza de jengibre

In your case, the problem is most likely the List<?>. GWT doesnt know what possible types you are putting in that list, so instead of generating code to serialize every single possible type on the sourcepath, it generates nothing except for what it knows it already needs. When you attempt to put something in there that wasn't needed elsewhere, the exception occurs, indicating that GWT wasn't told that you planned on sending that object over the wire.

A standard approach here is usually to create a marker interface that probably implements Serializable, y convertirlo en un List<MyModelObjects>. Then every object that can fit in there should implement that interface.

contestado el 22 de mayo de 12 a las 18:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.