简体   繁体   English

无法将Enum序列化为GWT

[英]Cannot serialize an Enum to GWT

I am unable to serialize an Enum to GWT if it implements java.io.Serializable. 如果它实现java.io.Serializable,我无法将Enum序列化为GWT。 It will GWT compile successfully, but at runtime, I get the dreaded: 它将成功编译GWT,但在运行时,我得到了可怕的:

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 键入“com .... security..AdminPrivilege”无法分配给“com.google.gwt.user.client.rpc.IsSerializable”并且没有自定义字段序列化程序。出于安全考虑,此类型不会被序列化。:instance =登录到控制台

If I implement com.google.gwt.user.client.rpc.IsSerializable it compiles and runs fine. 如果我实现com.google.gwt.user.client.rpc.IsSerializable,它编译并运行正常。 I am trying to avoid IsSerializable, since this Enum is persisted in our DB and is referenced in non-GWT servlets. 我试图避免IsSerializable,因为这个枚举在我们的数据库中持久存在,并在非GWT servlet中引用。 I do not want to introduce a GWT dependancy, even for that single class. 我不想引入GWT依赖性,即使对于那个单一类也是如此。

I've read most of the discussions on this topic here. 我在这里阅读了关于这个主题的大部分讨论。 I have: 我有:

  1. added a serialVersionUid (which should not be necessary) 添加了一个serialVersionUid(这不应该是必需的)
  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) - 添加了一个可调用的RPC方法,该方法返回Enum并将Enum的Set作为输入参数(尝试将此Enum放入白名单) -

For all other Enums, I generated a GWT version which implements IsSerializable. 对于所有其他枚举,我生成了一个实现IsSerializable的GWT版本。 But this new Enum is too complex to generate and I need the methods from the Enum in the GWT code. 但是这个新的Enum太复杂而无法生成,我需要GWT代码中的Enum方法。

Thanks for any help on this. 感谢您的帮助。

My Enum is below. 我的Enum在下面。 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. 如果你有,并且它有参数,你需要记住添加一个无参数构造函数,即使你不使用它,因为GWT将需要它。 Adding a parameterized constructor and forgetting to add a parameterless one gets me every time, at least with non-enum classes. 添加一个参数化的构造函数并忘记添加一个无参数的构造函数,每次都会得到我,至少对于非枚举类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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