How can I tell java.beans.Introspector to ignore a getter method?

我的梦境 提交于 2019-12-22 12:16:01

问题


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

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 ... }

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.


回答1:


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

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

EDIT: 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.



来源:https://stackoverflow.com/questions/7936025/how-can-i-tell-java-beans-introspector-to-ignore-a-getter-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!