Apache CXF With JAX-WS: Declaring Service Interface
Apache CXF With JAX-WS: Declaring Service Interface
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 −
1 of 9 03/04/20, 5:47 pm
Apache CXF with JAX-WS - 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>
2 of 9 03/04/20, 5:47 pm
Apache CXF with JAX-WS - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
<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>
</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>
3 of 9 03/04/20, 5:47 pm
Apache CXF with JAX-WS - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
<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 learnt how your service gets
routed and processed by a CXF servlet.
4 of 9 03/04/20, 5:47 pm
Apache CXF with JAX-WS - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
<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>
<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>
5 of 9 03/04/20, 5:47 pm
Apache CXF with JAX-WS - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
com.tutorialspoint.cxf.jaxws.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-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
6 of 9 03/04/20, 5:47 pm
Apache CXF with JAX-WS - 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 −
7 of 9 03/04/20, 5:47 pm
Apache CXF with JAX-WS - 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
8 of 9 03/04/20, 5:47 pm
Apache CXF with JAX-WS - Tutorialspoint https://www.tutorialspoint.com/apache_cxf/apac...
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.
9 of 9 03/04/20, 5:47 pm