【问题标题】:Is there a way to annotate an AutoBean property so that it will not be serialized/deserialized?有没有办法注释 AutoBean 属性,使其不会被序列化/反序列化?
【发布时间】:2012-12-12 21:49:18
【问题描述】:

我有一个具有仅 UI 需要的属性的 autobean。我相信您可以清空值,AutoBeanCodex 不会序列化该属性,但这相当于序列化时需要的额外步骤。

我希望有一些类似于 Editor @Ignore 注释的注释。例如:

public interface Foo {
    ...
    @Ignore
    String getUiOnlyProperty();
}

那么,除了在序列化时清空值之外,还有其他方法可以防止 autobean 属性被序列化吗?

【问题讨论】:

    标签: gwt autobean


    【解决方案1】:

    Autobeans 旨在成为 JSON/XML/任何格式的 Java 皮肤 - 它们并不是真正设计用于保存其他数据。也就是说,一些想法几乎可以使用开箱即用的工具回答您的问题,或者可能会激发一些其他想法来解决您的问题。

    您应该能够通过省略 setter 来构建只读属性。这不是您所要求的,但仍然可能很方便。

    按照这些思路,@PropertyName 注释的 JavaDoc 似乎暗示了这个可能的特性:

    /**
     * An annotation that allows inferred property names to be overridden.
     * <p>
     * This annotation is asymmetric, applying it to a getter will not affect the
     * setter. The asymmetry allows existing users of an interface to read old
     * {@link AutoBeanCodex} messages, but write new ones.
     */
    

    阅读旧消息但编写新消息似乎可能更接近您所追求的,并且仍然允许您使用看起来像 bean 的东西。

    真正的答案似乎是 AutoBean.setTaggetTag 方法:

    /**
     * A tag is an arbitrary piece of external metadata to be associated with the
     * wrapped value.
     * 
     * @param tagName the tag name
     * @param value the wrapped value
     * @see #getTag(String)
     */
    void setTag(String tagName, Object value);
    

    ...

    /**
     * Retrieve a tag value that was previously provided to
     * {@link #setTag(String, Object)}.
     * 
     * @param tagName the tag name
     * @return the tag value
     * @see #setTag(String, Object)
     */
    <Q> Q getTag(String tagName);
    

    AbstractAutoBean 中这些方法的实现可以看出,它们将数据存储在与通过网络发送的完全不同的对象中。缺点是您需要获取底层 AutoBean 对象(请参阅com.google.web.bindery.autobean.shared.AutoBeanUtils.getAutoBean(U) 了解执行此操作的一种方法)才能调用这些方法。

    【讨论】:

    • 我完全忽略了 get/setTag。那可以工作。但是,如果我有一个接口FooRaw 扩展了Foo 怎么办?然后,我可以在Foo.class 上解码,但在FooRaw.class 上进行编码。我没试过,但那应该只编码FooRaw中定义的属性吧?
    • 您可能需要在这些类型或其他东西之间有一个中间拆分表 - 您没有为 AutoBeanCodex.encode 方法指定类型,它只需要 autobean。 autobean 已经包含类型详细信息 - 它知道它代表什么类型。
    • 嗯,这是一组很好的选择。感谢您的帮助!
    【解决方案2】:

    解码为父接口的子类/接口在解码时不会爆炸,允许货物在编组步骤之前一起传递。我对下面实际代码的直接测试按预期执行。

      public interface ReplicateOptions {
        /**
         * Create target database if it does not exist. Only for server replications.
         */
        Boolean getCreateTarget();
    
        void setCreateTarget(Boolean create_target); 
    
      //baggage to pass along
      interface ReplicateCall<T> extends ReplicateOptions   {
    
          /**
           * If true starts subscribing to future changes in the source database and continue replicating them.
           */
          AsyncCallback<T> getContinuous();
    
          void setContinuous(AsyncCallback<T> continuous); 
    
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-26
      • 1970-01-01
      • 2011-01-12
      • 2015-03-17
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多