简体   繁体   中英

Cannot serialize an Enum to GWT

I am unable to serialize an Enum to GWT if it implements java.io.Serializable. It will GWT compile successfully, but at runtime, I get the dreaded:

Type 'com....security..AdminPrivilege' was not assignable to 'com.google.gwt.user.client.rpc.IsSerializable' and did not have a custom field serializer.For security purposes, this type will not be serialized.: instance = Login to Console

If I implement com.google.gwt.user.client.rpc.IsSerializable it compiles and runs fine. I am trying to avoid IsSerializable, since this Enum is persisted in our DB and is referenced in non-GWT servlets. I do not want to introduce a GWT dependancy, even for that single class.

I've read most of the discussions on this topic here. I have:

  1. added a serialVersionUid (which should not be necessary)
  2. added a no-arg constructor (but this is an Enum, so it must be private - I suspect this may be the problem)
  3. added a callable RPC method that returns the Enum and takes a Set of the Enum as an input argument (trying to get this Enum into the whitelist) -

For all other Enums, I generated a GWT version which implements IsSerializable. But this new Enum is too complex to generate and I need the methods from the Enum in the GWT code.

Thanks for any help on this.

My Enum is below. Notice it has an embedded Enum:

public enum AdminPrivilege implements java.io.Serializable {

    // Privileges
    MANAGE_XX("Manage XX", PrivilegeCategory.XX), 
    IMPORT_LICENSE("Import a License", PrivilegeCategory.XX), 
    SUBMIT_BUG("Submit a Bug", PrivilegeCategory.XX), 
    TEST_AD("Test AD", PrivilegeCategory.XX),

    // Administrator Privileges
    LOGIN("Login to XX", PrivilegeCategory.ADMIN), 
    MANAGE_ADMIN("Manage Administrators", PrivilegeCategory.ADMIN), 
    MANAGE_ROLE("Manage Roles", PrivilegeCategory.ADMIN),

    // Task Privileges
    CANCEL_TASK("Cancel Tasks", PrivilegeCategory.TASK), ;

    private static final long serialVersionUID = 1L;

    /**
     * Defines the privilege categories.
     * 
     */
    public enum PrivilegeCategory implements java.io.Serializable {

        XX("XX"), 
        ADMIN("Administrator"), 
        TASK("Task"), ;

        private static final long serialVersionUID = 2L;

        private String displayValue;

        // This constructor is required for GWT serialization
        private PrivilegeCategory() {
        }

        private PrivilegeCategory(String displayValue) {
            this.displayValue = displayValue;
        }

        @Override
        public String toString() {
            return displayValue;
        }
    }

    private String displayValue;
    private AdminPrivilege parentPrivilege;
    private PrivilegeCategory privilegeCategory;

    // This constructor is required for GWT serialization
    private AdminPrivilege() {
    }

    private AdminPrivilege(String displayValue, PrivilegeCategory category) {
        this.displayValue = displayValue;
        this.privilegeCategory = category;
    }

    private AdminPrivilege(String displayValue, PrivilegeCategory category, AdminPrivilege parent) {
        this(displayValue, category);
        this.parentPrivilege = parent;
    }

    /**
     * Return the parent privilege for this privilege.
     * 
     * @return
     */
    public AdminPrivilege getParentPrivilege() {
        return parentPrivilege;
    }

    /**
     * Return the category for this privilege.
     * 
     * @return
     */
    public PrivilegeCategory getPrivilegeCategory() {
        return privilegeCategory;
    }

    /**
     * Return the set of categories.
     * 
     * @return
     */
    public static Set<PrivilegeCategory> getPrivilegeCategories() {
        Set<PrivilegeCategory> category = new HashSet<PrivilegeCategory>();
        for (PrivilegeCategory c : PrivilegeCategory.values()) {
            category.add(c);
        }
        return category;
    }

    /**
     * Return the set of privileges for a category.
     * 
     * @return
     */
    public static Set<AdminPrivilege> getPrivileges(PrivilegeCategory category) {
        Set<AdminPrivilege> privileges = new HashSet<AdminPrivilege>();
        for (AdminPrivilege p : AdminPrivilege.values()) {
            if (category.equals(p.getPrivilegeCategory())) {
                privileges.add(p);
            }
        }
        return privileges;
    }

    /**
     * Return the set of child privileges for a specific privilege
     * 
     * @param parent
     * @return
     */
    public static Set<AdminPrivilege> getChildPrivileges(AdminPrivilege parent) {
        Set<AdminPrivilege> children = new HashSet<AdminPrivilege>();
        for (AdminPrivilege priv : values()) {
            if (parent.equals(priv.getParentPrivilege())) {
                children.add(priv);
            }
        }
        return children;
    }

    /**
     * Return the set of privileges that are parent privileges
     * 
     * @return
     */
        public static Set<AdminPrivilege> getParentPrivileges() {
            Set<AdminPrivilege> parents = new HashSet<AdminPrivilege>();
            for (AdminPrivilege priv : values()) {
                if (priv.getParentPrivilege() == null) {
                    parents.add(priv);
                }
            }
            return parents;
        }

    }

}

Have you specified a parameterized constructor in your enum? If you have, and it has parameters, you need to remember to add a no-parameters constructor as well, even if you don't use it, because GWT will need it. Adding a parameterized constructor and forgetting to add a parameterless one gets me every time, at least with non-enum classes.

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