CXF includes a simple frontend which builds services from reflection. This is in contrast to the JAX-WS frontend which requires you to annotate your services. The simple frontend will use reflection to intelligently map your classes to a WSDL model.
By default CXF uses the JAXB databinding. If you are interested in a databinding which does not require annotations, please see the documentation on the Aegis Databinding.
ServerFactoryBean
The ServerFactoryBean produces a Server instance for you. It requires a service class and an address to publish the service on. By creating a Server you'll have started your service and made it available to the outside world.
First you'll want to create your service class:
public interface Hello { String sayHello(); }
This does not have to be an interface, but we're going to use an interface for this example because
- It is good to separate out your service contract (your interface) from your implementation
- This allows us to easily create a Java Proxy client
We'll also need an implementation class:
public class HelloImpl implements Hello { public String sayHello() { return "hello"; } }
And now we'll want to create a Server from this
import org.apache.cxf.frontend.ServerFactoryBean; ... // Create our service implementation HelloImpl helloImpl = new HelloImpl(); // Create our Server ServerFactoryBean svrFactory = new ServerFactoryBean(); svrFactory.setServiceClass(Hello.class); svrFactory.setAddress("http://localhost:8080/Hello"); svrFactory.setServiceBean(helloImpl); svrFactory.create();
Your service is now started! You can access the wsdl at "http://localhost:8080/Hello?wsdl".
ClientProxyFactoryBean
You'll also want to create a client for your service. CXF includes a ClientProxyFactoryBean which will create a Java proxy for you from your interface which will invoke the service.
import org.apache.cxf.frontend.ClientProxyFactoryBean; ... ClientProxyFactoryBean factory = new ClientProxyFactoryBean(); factory.setServiceClass(Hello.class); factory.setAddress("http://localhost:8080/Hello"); Hello client = (Hello) factory.create();
You simply need to set your service class and your service's URL on the bean. Calling create() will then create a proxy for you based on your interface.