I use Apache CXF 2.4.2 and when I'm returnimg some object from the开发者_开发百科 database to user I want to exclude some properties (for instance, password). How I can do that without creating temporary class? Is there annotation for this?
As per @tomasz-nurkiewicz comment I should use @XmlTransient
annotation. But as noted in documentation
By default, if @XmlAccessorType on a class is absent, and none of its super classes is annotated with @XmlAccessorType, then the following default on the class is assumed:
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
Where XmlAccessType.PUBLIC_MEMBER
means that:
Every public getter/setter pair and every public field will be automatically bound to XML, unless annotated by XmlTransient. Fields or getter/setter pairs that are private, protected, or defaulted to package-only access are bound to XML only when they are explicitly annotated by the appropriate JAXB annotations.
So this is why @XmlTransient
for private field doesn't work in example from Tomasz Nurkiewicz. There are two possible ways to fix that:
1) Add annotation to public getter:
private String password;
@XmlTransient
public String getPassword() {
return password;
}
2) Add @XmlAccessorType
to class:
@XmlAccessorType(XmlAccessType.FIELD)
public User {
@XmlTransient
private String password;
}
Found at: http://old.nabble.com/@XmlTransient-ignored-td7406659.html
I assume you are using JAXB for object-XML mapping. In that case simply annotate fields you want to skip in your database entity with @XmlTransient
.
@XmlTransient
private String password;
However note that one day you will realize that you do need a temporary class mainly to decouple your CXF web service from the backend. After all you don't want to remember all the time that adding a column in the database immediately breaks the SOAP interface...
精彩评论