gpt4 book ai didi

gwt - 无法将枚举序列化为 GWT

转载 作者:行者123 更新时间:2023-12-05 00:09:47 28 4
gpt4 key购买 nike

如果 Enum 实现了 java.io.Serializable,我无法将它序列化为 GWT。它会成功编译 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.google.gwt.user.client.rpc.IsSerializable 它编译并运行良好。我试图避免 IsSerializable,因为这个 Enum 持久存在于我们的数据库中,并且在非 GWT servlet 中被引用。我不想引入 GWT 依赖关系,即使对于那个单一的类也是如此。

我在这里阅读了关于这个主题的大部分讨论。我有:
  • 添加了一个serialVersionUid(这应该不是必需的)
  • 添加了一个无参数构造函数(但这是一个枚举,所以它必须是私有(private)的 - 我怀疑这可能是问题)
  • 添加了一个可调用的 RPC 方法,该方法返回 Enum 并将 Enum 的 Set 作为输入参数(试图将此 Enum 放入白名单) -

  • 对于所有其他枚举,我生成了一个实现 IsSerializable 的 GWT 版本。但是这个新的 Enum 生成起来太复杂了,我需要 GWT 代码中 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;
    }

    }

    }

    最佳答案

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

    关于gwt - 无法将枚举序列化为 GWT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8056861/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com