简体   繁体   中英

GWT - SerializationException when I send Map<Object, Object>

I have some HashMap:

Map<SearchConfig, Object> searchParams;

There Object can be as simple type, such as String, or he can be simple entity:

public class SearchDataEntity implements Serializable  {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public SearchDataEntity() {}

    private String startDate;

    private String endDate;

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }
}

When I send this to the server I have SerializationException.

If I send SearchDataEntity separately from Map it's OK.

You tell the GWT compiler that a Map will contain an Object. Then you try to pass a String or a SearchDataEntity instead of an Object. The compiler did not expect that, and it did not include a way to process a String or a SearchDataEntity in this serialization policy.

Think of it this way: if you specify "Animal" instead of "Dog" for your Map, the compiler will not know how to process the method isBarking() when you pass a Dog object.

You need to use specific HashMap<SearchConfig, SearchDataEntity> or HashMap<SearchConfig, String> in your RPC calls.

On a separate note, the best practice is to use HashMap instead of Map for RPC calls. Again, the compiler tries to include as little code as possible. By specifying Map you force a compiler to do more work than necessary (it has to look for each case where this Map is used to see what implementations may be required), and it might results in a larger code than necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM