SOAP Web Service Server and Client Example With Apache CXF and Eclipse

Let’s create a simple standalone web service example with Apache CXF. First, we are going to build a server then generate a client to send a request to our web service.

1- Building a server

– Create a new Dynamic Web Project
– Right click the project and Configure > Convert to Maven Project
– Change build path source folder as src/main/java

Project Explorer looks like:
cxf2

package com.cxf.server;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
String sayHi(@WebParam(name = "text") String text);
}
view raw HelloWorld.java hosted with ❤ by GitHub
package com.cxf.server;
import javax.jws.WebService;
@WebService
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String name) {
System.out.println("say hello to " + name);
return "Hello " + name + "!";
}
}
view raw HelloWorldImpl.java hosted with ❤ by GitHub
package com.cxf.server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class Main {
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorldImpl();
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(HelloWorld.class);
factory.setAddress("http://localhost:9000/HelloWorld");
factory.setServiceBean(helloWorld);
factory.create();
System.out.println("Server ready...");
}
}
view raw Main.java hosted with ❤ by GitHub
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CXF_Server_Example</groupId>
<artifactId>CXF_Server_Example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.1.5</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
view raw pom.xml hosted with ❤ by GitHub

Run as Java Application and the server starts…
cxf3

We can test it our web service via SOAPUI
– Create a new SOAP project with initial WSDL address http://localhost:9000/HelloWorld?wsdl
cxf4

So it works!

2- Building a client

Initially, we should configure Eclipse to work with CXF.
– Install CXF binary from official site
– Set path via Eclipse > Window > Preferences > Web Services > CXF 2.x Preferences
cxf1

Then create a new project for client,

– Create a new Dynamic Web Project
– Right click the project and Configure > Convert to Maven Project
– Change build path source folder as src/main/java
– File > New > Other > Web Service Client

cxf5
cxf6

If you faced with an error like “Failed to read schema document ‘xjc.xsd’, because ‘file’ access is not allowed due to restriction set by the accessExternalSchema property” while creating a client, here is the solution:
http://stackoverflow.com/q/23011547/1091666

Project Explorer looks like:
cxf7

Create a Main class for the client and run it.

package com.cxf.client;
public class Main {
public static void main(String[] args) {
HelloWorldService service = new HelloWorldService();
String response = service.getHelloWorldPort().sayHi("client");
System.out.println("Service said: " + response);
}
}
view raw Main.java hosted with ❤ by GitHub

cxf8

That’s all folks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: