Slava Semushin Slava Semushin - 4 months ago 22
Java Question

CXF: how to exclude some properties when object sent through SOAP?

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?

Answer

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