如何告诉java.beans.Introspector忽略getter方法? [英] How can I tell java.beans.Introspector to ignore a getter method?

查看:123
本文介绍了如何告诉java.beans.Introspector忽略getter方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字段,它是两个值的组合。这是从JSON序列化并且运行良好的字段。

I have one field which is a composition of two values. This is the field that gets serialized to/from JSON and works great.

public String getRevisions() { return revisions; }
public void setRevisions(String revisions) { this.revisions = revisions; }

我添加了两个辅助方法来检索单独的值,但我不想将它们序列化为JSON。

I added two helper methods to retrieve the separate values, but I don't want them serialized to JSON.

public String getCurrentRevision() { ... return first revision ... }
public String getPreviousRevision() { ... return second revision ... }

有没有办法告诉 java.beans.Introspector 在通过 getBeanInfo()构建 BeanInfo 时忽略这些额外的getter code>?注释很可爱,我真的希望避免为它手动创建我自己的 BeanInfo

Is there a way I can tell java.beans.Introspector to ignore these additional getters when building the BeanInfo via getBeanInfo()? An annotation would be lovely, and I'm really hoping to avoid having to manually create my own BeanInfo for it.

推荐答案

也许从beans API中尝试这个注释: http://download.oracle.com/javase/7/docs/api/java/beans/Transient.html

Maybe try this annotation from the beans API: http://download.oracle.com/javase/7/docs/api/java/beans/Transient.html

他们建议你实际把它放在getter上,这似乎符合要求。

They advice you to actually place it on the getter, so that seems to fit the bill.

编辑:oops,只是注意到这是在Java 7中引入的。所以如果你想使用它,你需要确保它在最近的JRE中运行。

oops, just noticed this was introduced in Java 7. So you're gonna need to make sure this runs in a very recent JRE if you want to use it.

这篇关于如何告诉java.beans.Introspector忽略getter方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆