Apache CXF With POJO
Apache CXF with JAX-WS
Apache CXF with WSDL First
Apache CXF With JAX-RS
Apache CXF With JMS
Apache CXF Conclusion
Apache CXF Useful Resources
Apache CXF with POJO
In this chapter, you will learn how to develop a simple web application that sends a greetings message to the user. A web service project uses WSDL model. The CXF allows you to hide this WSDL model by providing a simple frontend to map Apache CXF APIs to the underlying WSDL.
In this simplest project, the interface of the web service will be directly exposed to the client and the client would use native Apache CXF APIs to call the web service.
First, we will create a web service. Every service has an interface that is exposed to the client. We may write this interface as a simple Apache CXF interface or as a WSDL document. In this Apache CXF-First approach, we will expose our service through a Apache CXF interface.
Developing Web Service
The service that we are going to create on the web will have a single web method called greetings. The method takes a string type argument in which we will send the user's name. The service will send back a greetings message to the caller with the received user name included in the message.
Web Service Interface
To expose the interface of our web service, we will create a Apache CXF interface as follows −
package com.tutorialspoint.cxf.pojo;
public interface HelloWorld {
String greetings(String text);
}
The interface has only one method called greetings. The server will implement this interface. In our trivial application, this interface is directly exposed to the client. Typically, in a web service application, you use WSDL to describe the web service interface. In this simple application, we will provide this direct interface to the client developer. The client would then call the greetings message on the server object. So first let us create the web service.
Web Service Implementation
The HelloWorld interface is implemented in the HelloWorldImpl Apache CXF class as shown below −
package com.tutorialspoint.cxf.pojo;
public class HelloWorldImpl implements HelloWorld {
@Override
public String greetings(String text) {
return "Hi " + text;
}
}
The greetings method receives a parameter of string type, appends it to a greeting message and returns the resultant string to the caller.
Next, we write the server application to host the HelloWorld service.
Creating Server
The server application consists of two parts −
The first part creates a factory for our web service, and
The second part writes a main method for instantiating it.
The server uses ServerFactoryBean class provided by CXF libraries to expose our HelloWorld interface to remote clients. Thus, we first instantiate the ServerFactoryBean class and then set its various properties −
ServerFactoryBean factory = new ServerFactoryBean();
We set the service class to be called by calling the setServiceClass method on the factory object −
factory.setServiceClass(HelloWorld.class);
We set the URL for calling our service by calling the factory's setAddress method. Note that the service will be published at this URL.
factory.setAddress("http://localhost:5000/Hello");
In this case, the service is deployed on the embedded server and will be listening to port 5000. You may opt for any port number of your choice.
Before creating the factory, you need to tell the factory about our service implementation class. This is done by calling the setServiceBean method on the factory object as shown here −
factory.setServiceBean(new HelloWorldImpl());
The service bean is set to the instance of our service implementation class. Finally, we create the factory by calling its create method −
factory.create();
Now, as we have developed the factory to run our web service, we will next write a main method to instantiate it and keep it running for some time.
Now, write a main method to instantiate the HelloServer class as follows −
public static void main(String[] args) throws Exception {
new HelloServer();
System.out.println("Listening on port 5000 ...");
}
Once instantiated, the HelloServer class will keep running indefinitely. For production deployments, you will definitely keep your server running forever. In the current situation, we would terminate the server after a predetermined time as follows −
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting ...");
System.exit(0);
The entire code for the HelloServer class is given below −
package com.tutorialspoint.cxf.pojo;
import org.apache.cxf.frontend.ServerFactoryBean;
public class HelloServer {
protected HelloServer() throws Exception {
ServerFactoryBean factory = new ServerFactoryBean();
factory.setServiceClass(HelloWorld.class);
factory.setAddress("http://localhost:5000/Hello");
factory.setServiceBean(new HelloWorldImpl());
factory.create();
}
public static void main(String[] args) throws Exception {
new HelloServer();
System.out.println("Listening on port 5000 ...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting ...");
System.exit(0);
}
}