Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

cxf

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?

like image 261
Slava Semushin Avatar asked Oct 10 '11 11:10

Slava Semushin


People also ask

What does Apache CXF use for integration with WSS4J and security?

CXF relies on WSS4J in large part to implement WS-Security. Within your own services, WS-Security can be activated by using WS-SecurityPolicy, which provides a comprehensive and sophisticated validation of the security properties of a received message.

What is CXF soap?

Apache CXF™ is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.

What is CXF interceptor?

Interceptors and Phases Interceptors are the fundamental processing unit inside CXF. When a service is invoked, an InterceptorChain is created and invoked. Each interceptor gets a chance to do what they want with the message. This can include reading it, transforming it, processing headers, validating the message, etc.

What is CXF in Apache Camel?

In Apache Camel, the Camel CXF component is the key to integrating routes with Web services. You can use the Camel CXF component to create a CXF endpoint, which can be used in either of the following ways: Consumer — (at the start of a route) represents a Web service instance, which integrates with the route.


2 Answers

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

like image 182
Slava Semushin Avatar answered Oct 02 '22 17:10

Slava Semushin


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

like image 28
Tomasz Nurkiewicz Avatar answered Oct 02 '22 17:10

Tomasz Nurkiewicz