Professional Documents
Culture Documents
You also have a choice of different transports such as HTTP, JMS , JBI and the choice
of front-end API's like JAX-RS and JAX-WS . Having so many options for web service
development, there is a need for an open source services framework to glue all the above
mentioned options together and that is what Apache CXF does.
In this tutorial, you will learn how to use CXF to create both a web service and a client that
consumes the service, using one or more of the options that we have listed above. This
tutorial will walk you through the entire code development for both server and the client. As
each application can use only one of the options from each category, namely frontend,
transport and protocol, considering all permutations and combinations of these three, the
number of applications will be exorbitantly high.
To keep it simple, we have used maven with its command line interface. You may use your
preferred IDE for creating a maven project.
In the next chapter, let us get started with the first one.
1 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
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.
//HelloWorld.java
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.
//HelloWorldImpl.java
package com.tutorialspoint.cxf.pojo;
public class HelloWorldImpl implements HelloWorld {
@Override
public String greetings(String text) {
2 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
Creating Server
The server application consists of two parts −
The first part creates a factory for our web service, and
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 −
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();
3 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
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.
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);
//HelloServer.java
//HelloServer.java
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);
}
}
The server application that we have created uses ServerFactoryBean class from CXF
libraries. We must now include these libraries in our project to successfully compile the
HelloServer class. We will use Maven to set up the project dependencies.
4 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
mvn archetype:generate
On the completion of the maven command, you will find the appropriate folder structure
created in your current folder along with pom.xml file.
You will add the CXF dependencies in the pom.xml and also copy the above created
Apache CXF files into the appropriate folder of the maven created structure. For your ready
reference, we have given below the pom.xml file for the project that we created on our
5 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
machine.
<profiles>
<profile>
<id>server</id>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>
com.tutorialspoint.cxf.pojo.HelloServer
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>client</id>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
6 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
<goal>java</goal>
</goals>
<configuration>
<mainClass>
com.tutorialspoint.cxf.pojo.HelloClient
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
<version>3.3.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-simple</artifactId>
<version>3.3.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.3.0</version>
</dependency>
<!-- Jetty is needed if you're using the CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
7 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
</properties>
</project>
The above pom.xml may include additional dependencies which are irrelevant to this
project, but are required for our next project in this tutorial. Anyway, there is no harm as
such in including additional dependencies.
Running Server
To build the project, use the following command in your command-line window −
mvn -Pserver
This will start the server and you will see the following prompt on the console −
8 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
Now, in your browser window specify the URL of our published service. You will see the
following output −
This confirms that our service is running at the specified port on a localhost. Since we did
not specify the greetings message in our call, a SOAP fault message is returned to the
browser.
You may further test your web service using a SOAP client of your choice. Here we have
used Postman to test our server.
9 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
Observe that SOAP Request was hand-coded. After posting the request, the server sent a
SOAP Response message, which is seen in the bottom portion of the screen shot.
From this, you can understand that CXF maintains the use of SOAP protocols for both
request and response while providing you a unified view to a variety of web technologies
that do exist in today's world. This greatly simplifies the web application development.
Our next task is to create a client that will consume the web service that you have created.
Creating Client
In the server application HelloWorld is the interface that exposes our web service. The web
service itself just provides a plain greeting message to the client. Usually, the web service
interface is exposed to the outside world using WSDL (Web Services Description
Language). In this trivial application, we will expose our web service to the client by
exposing directly the service interface and that is the HelloWorld.class.
For this purpose, CXF provides a factory class called ClientProxyFactoryBean that allows
us to attach to the desired interface to the created factory instance.
We call the setAddress method on the factory bean instance to set the URL by which our
web service can be invoked. In our case, we will use the URL used while creating the server
in our earlier step −
factory.setAddress("http://localhost:5000/Hello");
Next, we call the create method on the factory instance to attach our service interface
HelloWorld.class to it.
Finally, we call the greetings method to invoke the remote web service.
System.out.println(helloServer.greetings(System.getProperty("user.name")));
10 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
//HelloClient.java
package com.tutorialspoint.cxf.pojo;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
public class HelloClient {
public static void main(String[] args) throws Exception {
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setAddress("http://localhost:5000/Hello");
HelloWorld helloServer = factory.create(HelloWorld.class);
System.out.println(helloServer.greetings(System.getProperty("user.name")));
}
}
Running Client
Make sure that the server is still running on your machine. In case, if it has timed out, restart
the server with the following command −
mvn -Pserver
Now, before the server times out which we have set to 5 minutes, open another command
line window and start the client with the following command −
mvn -Pclient
You will see a message similar to the following on the command line −
Hi tutorialspoint
Note that tutorialspoint is our user name. You will get a greeting with your own name.
In the next chapter, we will learn how to use CXF in a JAX-WS (Apache CXF API for XML
Web Services) project.
11 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
In this JAX-WS application, we will use Apache CXF-first approach like the earlier POJO
application. So first we will create an interface for our web service.
//HelloWorld.java
package com.tutorialspoint.cxf.jaxws.helloworld;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
String greetings(String text);
}
We annotate the interface with a @WebService tag. Next, we will implement this interface.
//HelloWorldImpl.java
package com.tutorialspoint.cxf.jaxws.helloworld;
public class HelloWorldImpl implements HelloWorld {
@Override
public String greetings(String name) {
return ("hi " + name);
}
}
The greetings method is annotated with @Override tag. The method returns a "hi" message
to the caller.
Developing Server
Unlike the POJO application, we will now decouple the interface by using the CXF supplied
Endpoint class to publish our service. This is done in the following two lines of code −
12 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
);
The first parameter of the publish method specifies the URL at which our service will be
made available to the clients. The second parameter specifies the implementation class for
our service. The entire code for the server is shown below −
//Server.java
package com.tutorialspoint.cxf.jaxws.helloworld;
import javax.xml.ws.Endpoint;
import org.apache.cxf.ext.logging.LoggingFeature;
public class Server {
public static void main(String[] args) throws Exception {
HelloWorld implementor = new HelloWorldImpl();
Endpoint.publish("http://localhost:9090/HelloServerPort",
implementor,
new LoggingFeature());
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting ...");
System.exit(0);
}
}
To deploy our server, you will need to make few more modifications to your project as listed
below.
Deploying Server
Finally, to deploy the server application, you will need to make one more modification in
pom.xml to setup your application as a web application. The code that you need to add into
your pom.xml is given below −
<profiles>
<profile>
<id>server</id>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
13 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
</goals>
<configuration>
<mainClass>
com.tutorialspoint.cxf.jaxws.helloworld.Server
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Before you deploy the application, you need to add two more files to your project. These are
shown in the screenshot below −
These files are CXF standard files which define the mapping for CXFServlet. The code
within the web.xml file is shown here for your quick reference −
//Web.xml
<?xml version = "1.0" encoding = "UTF-8"??>
<web-app xmlns = "http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>cxf</display-name>
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>
1
</load-on-startup>
</servlet>
<servlet-mapping>
14 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
<servlet-name>
cxf
</servlet-name>
<url-pattern>
/services/*
</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
In the cxf-servlet.xml, you declare the properties for your service's endpoint. This is shown
in the code snippet below −
<beans ...>
<jaxws:endpoint xmlns:helloworld = "http://tutorialspoint.com/"
id = "helloHTTP"
address = "http://localhost:9090/HelloServerPort"
serviceName = "helloworld:HelloServiceService"
endpointName = "helloworld:HelloServicePort">
</jaxws:endpoint>
</beans>
Here we define the id for our service endpoint, the address on which the service will be
available, the service name and the endpoint name. Now, you learnt how your service gets
routed and processed by a CXF servlet.
15 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
</properties>
<profiles>
<profile>
<id>server</id>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>
com.tutorialspoint.cxf.jaxws.helloworld.Server
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>client</id>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
<goals>
<configuration>
<mainClass>
com.tutorialspoint.cxf.jaxws.helloworld.Client
</mainClass>
</configuration>
</execution>
</executions>
16 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
</project>
Note that it also includes a profile for building client that we will be learning in the later
sections of this tutorial.
mvn -Pserver
17 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
Like earlier, you can test the server by opening the server URL in your browser.
As we did not specify any operation, only a fault message is returned to the browser by our
application.
Now, try adding the ?wsdl to your URL and you will see the following output −
18 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
So our server application is running as expected. You may use the SOAP Client such as
Postman described earlier to further test your service.
In the next section, we will learn how to write a client that uses our service.
Developing Client
Writing the client in a CXF application is as trivial as writing a server. Here is the complete
code for the client −
//Client.java
package com.tutorialspoint.cxf.jaxws.helloworld;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
public final class Client {
private static final QName SERVICE_NAME
= new QName("http://helloworld.jaxws.cxf.tutorialspoint.com/",
"HelloWorld");
private static final QName PORT_NAME
= new QName("http://helloworld.jaxws.cxf.tutorialspoint.com/",
"HelloWorldPort");
private Client() {
}
public static void main(String[] args) throws Exception {
Service service = Service.create(SERVICE_NAME);
System.out.println("service created");
String endpointAddress = "http://localhost:9090/HelloServerPort";
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING,
endpointAddress);
HelloWorld hw = service.getPort(HelloWorld.class);
System.out.println(hw.greetings("World"));
}
}
Here, we use the CXF supplied Service class to bind to the known service. We call the
create method on the Service class to get an instance of the service. We set the known
port by calling the addPort method on the service instance.
Now, we are ready to consume the service, which we do by first obtaining the service
interface by calling the getPort method on the service instance. Finally, we call our
greetings method to print the greetings message on the console.
Now, as you have learned the basics of CXF by using the Apache CXF-First approach, you
will now learn how to use CXF with WSDL-First approach in our next chapter.
19 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
We write the web service interface in a WSDL document which is XML-based. We will use a
tool to map this WSDL to Apache CXF interfaces which are then implemented and used by
our client and server applications. For providing decoupling, starting with a WSDL is a
preferred way. For this, you need to first learn a new language - WSDL. Writing WSDL
needs a careful approach and it would be better if you can gain some understanding on this
before you start working on it.
In this lesson, we will start by defining a web service interface in a WSDL document. We will
learn how to use CXF to create both server and client applications starting with WSDL. We
will keep the application simple to maintain focus on the use of CXF. After the server
application is created we will publish it to a desired URL using a built-in CXF class.
//Hello.wsdl
<?xml version = "1.0" encoding = "UTF-8"?>
<wsdl:definitions xmlns:soap = "http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns = "http://helloworld.tutorialspoint.com/"
xmlns:wsdl = "http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
name = "HelloWorld"
targetNamespace = "http://helloworld.tutorialspoint.com/">
<wsdl:types>
<xsd:schema attributeFormDefault = "unqualified"
elementFormDefault = "qualified"
targetNamespace = "http://helloworld.tutorialspoint.com/">
<xsd:element name = "greetings" type = "tns:greetings"/>
<xsd:complexType name = "greetings">
<xsd:sequence>
<xsd:element minOccurs = "0" name = "arg0" type = "xsd:string"/>
</xsd:sequence>
20 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
</xsd:complexType>
<xsd:element name = "greetingsResponse"
type = "tns:greetingsResponse"/>
<xsd:complexType name = "greetingsResponse">
<xsd:sequence>
<xsd:element minOccurs = "0" name = "return" type = "xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
<wsdl:message name = "greetings">
<wsdl:part element = "tns:greetings" name = "parameters"> </wsdl:part>
</wsdl:message>
<wsdl:message name = "greetingsResponse">
<wsdl:part element = "tns:greetingsResponse" name = "parameters"> </wsdl:part>
</wsdl:message>
<wsdl:portType name = "HelloWorldPortType">
<wsdl:operation name = "greetings">
<wsdl:input message = "tns:greetings" name = "greetings"> </wsdl:input>
<wsdl:output message = "tns:greetingsResponse" name = "greetingsResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name = "HelloWorldSoapBinding" type = "tns:HelloWorldPortType">
<soap:binding style = "document"
transport = "http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name = "greetings">
<soap:operation soapAction = "" style = "document"/>
<wsdl:input name = "greetings"></wsdl:input>
<wsdl:output name = "greetingsResponse">
<soap:body use = "literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name = "HelloWorldService">
<wsdl:port binding = "tns:HelloWorldSoapBinding" name = "HelloWorldPort">
<soap:address location = "http://localhost:9090/HelloServerPort"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
Note that writing a syntactically correct wsdl has always been a challenge to the developers;
there are many tools and online editors are available for creating a wsdl. These editors ask
for the names of messages that you want to implement along with the parameters that you
wish to pass in a message and the type of return message that you want your client
application to receive. If you know wsdl syntax, you may hand code the entire document or
use one of the editors to create your own.
In the above wsdl, we have defined a single message called greetings. The message is
21 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
With this, we will now proceed to server development. Before developing the server, we
need to generate Apache CXF interface to our web service. This is to be done from the
given wsdl. To do this, you use a tool called wsdl2java.
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<wsdlOptions>
<wsdlOption>
<wsdl>src/main/resources/hello.wsdl</wsdl>
<faultSerialVersionUID> 1 </faultSerialVersionUID>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Note that we specify the location of the wsdl file as src/main/resources/Hello.wsdl. You
will have to make sure that you create an appropriate directory structure for your project and
add the earlier shown hello.wsdl file to the specified folder.
The wsdl2java plugin will compile this wsdl and create Apache CXF classes in a pre-
defined folder. The full project structure is shown here for your ready reference.
22 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
Now, you are ready to create a server using the wsdl2java generated classes. The classes
that wsdl2java has created is shown in the figure below −
23 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
In the list of generated classes, you must have noticed one of them is a Apache CXF
interface - this is HelloWorldPortType.java. Examine this file in your code editor. The file
contents are shown here for your ready reference −
//HelloWorldPortType.java
package com.tutorialspoint.helloworld;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
/**
* This class was generated by Apache CXF 3.3.0
* 2019-02-11T12:05:55.220+05:30
* Generated source version: 3.3.0
*
*/
@WebService(targetNamespace = "http://helloworld.tutorialspoint.com/",
name = "HelloWorldPortType")
@XmlSeeAlso({ObjectFactory.class})
public interface HelloWorldPortType {
@WebMethod
@RequestWrapper(localName = "greetings", targetNamespace =
"http://helloworld.tutorialspoint.com/", className =
"com.tutorialspoint.helloworld.Greetings")
@ResponseWrapper(localName = "greetingsResponse", targetNamespace =
"http://helloworld.tutorialspoint.com/", className =
"com.tutorialspoint.helloworld.GreetingsResponse")
@WebResult(name = "return", targetNamespace =
"http://helloworld.tutorialspoint.com/")
public java.lang.String greetings(
@WebParam(name = "arg0", targetNamespace =
"http://helloworld.tutorialspoint.com/")
java.lang.String arg0
);
}
Note that the interface contains a method called greetings. This was a message type in our
wsdl. The wsdl2java tool has added this method to the generated interface. Now, you can
understand that whatever messages you write in your wsdl, a corresponding method would
be generated in the interface.
Now, your task would be to implement all these methods corresponding to the various
messages that you have defined in your wsdl. Note that in the earlier example of Apache
CXF-First, we started out with a Apache CXF interface for our web service. In this case, the
24 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
//HelloWorldImpl.java
package com.tutorialspoint.helloworld;
public class HelloWorldImpl implements HelloWorldPortType {
@Override
public String greetings(String name) {
return ("hi " + name);
}
}
The code implements the sole interface method called greetings. The method takes one
parameter of string type, prepends a "hi" message to it and returns the resultant string to
the caller.
Developing Server
Developing server application is once again trivial. Here, we will use the CXF supplied
Endpoint class to publish our service. This is done in the following two lines of code −
//Server.java
package com.tutorialspoint.helloworld;
import javax.xml.ws.Endpoint;
import org.apache.cxf.ext.logging.LoggingFeature;
public class Server {
public static void main(String[] args) throws Exception {
HelloWorldPortType implementor = new HelloWorldImpl();
Endpoint.publish("http://localhost:9090/HelloServerPort",
implementor,
new LoggingFeature());
25 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}
To build this server class you will need to add a build profile in your pom.xml. This is shown
below −
<profile>
<id>server</id>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>
com.tutorialspoint.helloworld.Server
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
</profile>
Note that the fully qualified name of the Server class is specified in the configuration. Also,
the dependency tag specifies that we will be using the embedded jetty web server to deploy
our server application.
26 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
Deploying Server
Finally, to deploy the server application, you will need to make one more modification in
pom.xml to setup your application as a web application. The code that you need to add into
your pom.xml is given below −
<defaultGoal>install</defaultGoal>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
<webResources>
<resource>
<directory>src/main/resources</directory>
<targetPath>WEB-INF</targetPath>
<includes>
<include>*.wsdl</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</pluginManagement>
Before you deploy the application, you need to add two more files to your project. These are
shown in the screenshot below −
These files are CXF standard files which define the mapping for CXFServlet. The code
within the web.xml file is shown here for your quick reference −
//cxf-servlet.xml
<web-app xmlns = "http://java.sun.com/xml/ns/javaee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" version="2.5"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
27 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>cxf</display-name>
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
In the cxf-servlet.xml you declare the properties for your service's endpoint. This is shown
in the code snippet below −
<beans ...>
<jaxws:endpoint xmlns:helloworld = "http://tutorialspoint.com/"
id="helloHTTP"
address = "http://localhost:9090/HelloServerPort"
serviceName = "helloworld:HelloServiceService"
endpointName = "helloworld:HelloServicePort">
</jaxws:endpoint>
</beans>
Here we define the id for our service endpoint, the address on which the service will be
available, the service name and the endpoint name. Now, you understand how your service
gets routed and processed by a CXF servlet.
28 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
<artifactId>cxf-wsdl</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<defaultGoal>install</defaultGoal>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
<webResources>
<resource>
<directory>src/main/resources</directory>
<targetPath>WEB-INF</targetPath>
<includes>
<include>*.wsdl</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<wsdlOptions>
<wsdlOption>
<wsdl>src/main/resources/Hello.wsdl</wsdl>
<faultSerialVersionUID>1</faultSerialVersionUID>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
29 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>server</id>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>
com.tutorialspoint.helloworld.Server
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>client</id>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
30 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>
com.tutorialspoint.helloworld.Client
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-management</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-metrics</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf.xjc-utils</groupId>
<artifactId>cxf-xjc-runtime</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
31 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.8.0-beta2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
</project>
Note that it also includes a profile for building client that we will be learning soon in the later
sections.
This will generate the appropriate Apache CXF classes from your wsdl, compile your
Apache CXF classes, deploy the server on the embedded jetty server and run your
application.
As before, you can test the server by opening the server URL in your browser.
32 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
As we did not specify any operation, only a fault message is returned to the browser by our
application. Now, try adding the ?wsdl to your URL and you will see the following output −
So our server application is running as expected. You may use the SOAP Client such as
33 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
The next part of this tutorial is to write a client that uses our service.
Developing Client
Writing the client in a CXF application is as important as writing a server. Here is the
complete code for the client that essentially consists of only three lines, the rest of the lines
just print the service information to the user.
//Client.java
package com.tutorialspoint.helloworld;
public class Client {
public static void main(String[] args) throws Exception {
//Create the service client with its default wsdlurl
HelloWorldService helloServiceService = new HelloWorldService();
System.out.println("service: " +
helloServiceService.getServiceName());
System.out.println("wsdl location: " +
helloServiceService.getWSDLDocumentLocation());
HelloWorldPortType helloService =
helloServiceService.getHelloWorldPort();
System.out.println(helloService.greetings
(System.getProperty("user.name")));
}
}
Here, we simply create an instance of our service HelloWorldService, get its port by calling
getHelloWorldPort method, and then pass our greetings message to it. Run the client and
you will see the following output −
service: {http://helloworld.tutorialspoint.com/}HelloWorldService
wsdl location: file:/Users/drsarang/Desktop/tutorialpoint/cxf-
wsdl/src/main/resources/Hello.wsdl
hi drsarang
So far you have learned how to use CXF with Apache CXF-First and WSDL-First
architectures. In the Apache CXF-First approach, you used a POJO with
ServerFactoryBean class from CXF libraries to create a server. To create a client you used
ClientProxyFactoryBean class from CXF library. In the WSDL-First approach, you used
Endpoint class to publish the service at the desired URL and a specified implementor. You
can now extend these techniques to integrate different protocols and transports.
34 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
Before proceeding ahead into this chapter, we assume that you know how to write a
RESTful web service in Java. I will show you how to use CXF on top of this JAX-RS (Java
API for RESTful Web Services) . We will create a web service that maintains a list of latest
movies. When the user requests a movie, he specifies the movie ID in his request, the
server will locate the movie and return it to the client. In our trivial case, we will simply return
the movie name to the client and not the actual binary MP4 file. So let us start creating a
JAX-RS application.
//Movie.java
package com.tutorialspoint.cxf.jaxrs.movie;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Movie")
public class Movie {
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Note the use of XmlRootElement tag to declare the XML element for the Movie tag. Next,
we will create a service that holds the list of movies in its database.
//MovieService.java
35 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
package com.tutorialspoint.cxf.jaxrs.movie;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
@Path("/movieservice/")
@Produces("text/xml")
public class MovieService {
long currentId = 123;
Map<Long, Movie> movies = new HashMap<>();
public MovieService() {
init();
}
@GET
@Path("/movie/{id}/")
public Movie getMovie(@PathParam("id") String id) {
long idNumber = Long.parseLong(id);
return movies.get(idNumber);
}
final void init() {
Movie c1 = new Movie();
c1.setName("Aquaman");
c1.setId(1001);
movies.put(c1.getId(), c1);
36 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
Note that we use the following two annotations to specify the URL path for our movie service
and its return type −
@Path("/movieservice/")
@Produces("text/xml")
We use the @GET and @Path annotations to specify the URL for the GET request as
follows −
@GET
@Path("/movie/{id}/")
The movie database itself is initialized in the init method, where we add five movie items to
the database.
Developing Server
To create a server, we use CXF supplied JAXRSServerFactoryBean class.
factory.setResourceClasses(Movie.class);
factory.setResourceClasses(MovieService.class);
factory.setResourceProvider(MovieService.class,
new SingletonResourceProvider(new MovieService()));
factory.setAddress("http://localhost:9000/");
Finally, we publish the server by calling the create method on the factory instance.
factory.create();
//Server.java
37 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
package com.tutorialspoint.cxf.jaxrs.movie;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
public class Server {
public static void main(String[] args) throws Exception {
JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
factory.setResourceClasses(Movie.class);
factory.setResourceClasses(MovieService.class);
factory.setResourceProvider(MovieService.class,
new SingletonResourceProvider(new MovieService()));
factory.setAddress("http://localhost:9000/");
factory.create();
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
38 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>
com.tutorialspoint.cxf.jaxrs.movie.Server
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>client</id>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>
com.tutorialspoint.cxf.jaxrs.movie.Client
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
39 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
</build>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.7</version>
</dependency>
</dependencies>
</project>
Developing Client
Writing the RS client is trivial. We simply create a URL object and open its stream. We use
CXF supplied IOUtils class to copy the contents of input stream to a local stream.
//Client.java
package com.tutorialspoint.cxf.jaxrs.movie;
40 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
import java.io.InputStream;
import java.net.URL;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.io.CachedOutputStream;
public class Client {
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:9000/movieservice/movie/1002");
try (InputStream instream = url.openStream();
CachedOutputStream outstream = new CachedOutputStream()) {
IOUtils.copy(instream, outstream);
String str = outstream.getOut().toString();
System.out.println(str);
}
}
}
mvn -Pserver
http://localhost:9000/movieservice/movie/1002
You may invoke the service by using a Java client application that we have developed by
running the following command in a separate command-line window.
41 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
mvn -Pclient
CXF samples provides several examples on how to use CXF with JAX-RS. The interested
readers are encouraged to study these samples.
As earlier, we will first create a sample server application that provides a singular web
method called sayHi.
//HelloWorld.java
package com.tutorialspoint.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
@WebMethod
String sayHi(@WebParam(name = "name") String name);
}
Implementing Service
The implementation of the service interface is defined as follows −
//HelloWorldImpl.java
42 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
package com.tutorialspoint.service.impl;
import javax.jws.WebService;
import com.tutorialspoint.service.HelloWorld;
@WebService
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHi(String name) {
return "Hello " + name;
}
}
The implementation simply returns a Hello message to the user. As you see, the interface
and its implementation are similar to all the earlier projects in this tutorial that you have
studied so far.
Now, comes the most important point which is to create a server application that sets up a
message queue and keeps on listening to the incoming messages.
Creating Server
In the server application, first we create a JMS end point as follows −
Note that we set up a queue on a specified port that lives for a specified amount of time. We
now create a messaging service by instantiating
org.apache.activemq.broker.BrokerService class. This is a server class for ActiveMQ
messaging server.
You may use any other messaging server of your choice other than ActiveMQ. We now
connect this server to a desired URI.
broker.addConnector("tcp://localhost:61616");
We set up the directory for the data storage of the incoming messages −
43 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
broker.setDataDirectory("target/activemq-data");
broker.start();
Next, we create an instance of our service bean HelloWorld using the server factory bean
class as used in our earlier POJO application −
Next, we set up the JMS endpoint on the factory so that the factory will keep on listening to
the incoming messages −
factory.setTransportId
(JMSSpecConstants.SOAP_JMS_SPECIFICATION_TRANSPORTID);
factory.setAddress(JMS_ENDPOINT_URI);
Finally, we set up the implementer class in the factory and start running it −
factory.setServiceBean(implementor);
factory.create();
At this point your server is up and running. Note that since we have used the factory bean
class as in the POJO application, the need for CXFServlet and the web.xml file is not
required.
//ServerJMS.java
package com.tutorialspoint.server;
import java.util.Collections;
import org.apache.cxf.ext.logging.LoggingFeature;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.transport.jms.spec.JMSSpecConstants;
import com.tutorialspoint.service.HelloWorld;
import com.tutorialspoint.service.impl.HelloWorldImpl;
import org.apache.activemq.broker.BrokerService;
44 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}
Adding Dependencies
The server application that we have created uses ActiveMQ messaging server. Thus, you
will need to add few more dependencies to your project. The complete pom.xml file is shown
here for you to understand the additional needed dependencies.
45 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<profiles>
<profile>
<id>server</id>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>
com.tutorialspoint.server.ServerJMS
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>client</id>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>
46 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
com.tutorialspoint.client.ClientJMS
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
<version>5.15.8</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-kahadb-store</artifactId>
<version>5.15.8</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-jms</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
</project>
47 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
Running Server
To start running the server, as in the earlier cases, type the following command in your
command window −
mvn -Pserver
This will start the ActiveMQ message server, set up the messaging queue and create a
factory bean that keeps listening to this queue.
Creating Client
In the client application, first we set up the JMS endpoint same as the one used in the server
application −
factory.setTransportId (JMSSpecConstants.SOAP_JMS_SPECIFICATION_TRANSPORTID);
factory.setAddress (JMS_ENDPOINT_URI);
HelloWorld client = factory.create(HelloWorld.class);
Finally, we call the service method and print its resultant output −
// ClientJMS.java
package com.tutorialspoint.client;
48 of 49 03/04/20, 5:49 pm
Apache CXF - Quick Guide - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
import com.tutorialspoint.service.HelloWorld;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.jms.spec.JMSSpecConstants;
The WSDL provides an XML representation of your service interface. You used wsdl2java
tool to create Java interfaces from WSDL and finally wrote both the server and the client
using the created interfaces. The tutorial also briefly introduced you to use CXF in your
RESTful web service application. Finally, we also discussed how CXF can be used with
JMS. You may now refer to CXF-samples for further study.
Note − The entire project source code may be downloaded from here.
49 of 49 03/04/20, 5:49 pm