无法将Enum序列化为GWT

时间:2011-11-08 20:45:53

标签: gwt serialization enums

如果它实现java.io.Serializable,我无法将Enum序列化为GWT。它将成功编译GWT,但在运行时,我得到了可怕的:

  

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

如果我实现com.google.gwt.user.client.rpc.IsSerializable,它会编译并运行正常。我试图避免IsSerializable,因为这个枚举在我们的数据库中持久存在,并在非GWT servlet中引用。我不想引入GWT依赖性,即使对于那个单一类也是如此。

我在这里阅读了关于这个主题的大部分讨论。我有:

  1. 添加了serialVersionUid(这不是必需的)
  2. 添加了一个无参数构造函数(但这是一个枚举,所以它必须是私有的 - 我怀疑这可能是问题)
  3. 添加了一个可调用的RPC方法,该方法返回Enum并将Enum的Set作为输入参数(尝试将此Enum放入白名单中) -
  4. 对于所有其他枚举,我生成了一个实现IsSerializable的GWT版本。但是这个新的Enum太复杂而无法生成,我需要GWT代码中Enum的方法。

    感谢您提供任何帮助。

    我的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;
            }
    
        }
    
    }
    

1 个答案:

答案 0 :(得分:1)

您是否在枚举中指定了参数化构造函数?如果你有,并且它有参数,你需要记住添加一个无参数构造函数,即使你不使用它,因为GWT将需要它。添加一个参数化的构造函数并忘记添加一个无参数的构造函数,每次都会得到我,至少对于非枚举类。