How can I tell java.beans.Introspector to ignore a

2019-05-19 03:52发布

问题:

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.