CXF Overview
Apache CXF is an open source, fully featured Web services framework, its name CXF is originated from Celtix and XFire, the two projects are nicely combined work together to Apache. CXF supports JAX-WS, Binding, DataBinding and Transport implementation, the nice feature is its embeddable Web service component: (e.g. integrated Spring Framework and Geronimo), CXF has been designed to provide a pluggable architecture that supports not only XML but also non-XML type bindings, such as JSON and CORBA, in combination with any type of transport. in this how-to, we look though a step-by-step hello world example with CXF to start the first web service project.
Prerequisites
please hava JDK installed and download the CXF latest release version from apache website.
1. Create a project
Use IDE(Eclipse/Idea), Create a new Java project and set below dependency libraries as classpath, Unpack the CXF download distribution file, add the following mandatory jars to dependency libraries of project in order to use full CXF functionality.
4. geronimo-javamail_1.4_spec-1.6.jar (or Sun’s JavaMail jar)
5. geronimo-servlet_2.5_spec-1.2.jar (or Sun’s Servlet jar)
6. geronimo-ws-metadata_2.0_spec-1.1.2.jar (JSR 181)
7. geronimo-jaxws_2.1_spec-1.0.jar (or Sun’s jaxws-api-2.1.jar)
8. geronimo-stax-api_1.0_spec-1.0.1.jar (or other stax-api jar)
9. jaxb-api-2.1.jar
10. jaxb-impl-2.1.12.jar
11. jetty-6.1.21.jar
12. jetty-util-6.1.21.jar
13. neethi-2.0.4.jar
14. saaj-api-1.3.jar
15. saaj-impl-1.3.2.jar
16. wsdl4j-1.6.2.jar
17. wstx-asl-3.2.8.jar
18. XmlSchema-1.4.5.jar
19. xml-resolver-1.2.jar
20. cxf-2.2.2.jar
2. Create a web service interface “IHelloWorld” with one method sayHi.
@WebService public interface IHelloWorld { //@WebParam is optional String sayHi(@WebParam(name="text") String text); }
3. Implement this webservice interface with Annotation @WebService.
@WebService public class HelloWorldImpl implements IHelloWorld { public String sayHi(String name) { System.out.println("sayHello is called by " + name); return "Hello " + name; } }
4. Create a webservice server
Bean JaxWsServerFactoryBean
is to help easily create Server endpoints for JAX-WS, we instantiate JaxWsServerFactoryBean
and set service class, service bean and address, method create()
will start a server and register it with the ServerManager.
public class Server { private Server() { IHelloWorld helloWorld = new HelloWorldImpl(); //create WebService service factory JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean(); //register WebService interface factory.setServiceClass(IHelloWorld.class); //publish the interface factory.setAddress("http://localhost:9000/HelloWorld"); factory.setServiceBean(helloWorld); //create WebService instance factory.create(); } public static void main(String[] args) throws InterruptedException { //now start the webservice server new Server(); System.out.println("Server ready..."); Thread.sleep(1000 * 60); System.out.println("Server exit..."); System.exit(0); } }
in IDE or use Command Line, runing the static method main
to start this web service, once it is started, type URL http://localhost:9000/HelloWorld?wsdl
in address bar of browser, the WSDL should be displayed as below.
<wsdl:definitions name="IHelloWorldService" targetNamespace="http://asjava.com/"> <ul> <li><wsdl:types></li> <li><xsd:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://asjava.com/"></li> <li><xsd:element name="sayHi" type="tns:sayHi"/></li> <li><xsd:complexType name="sayHi"></li> <li><xsd:sequence></li> <li><xsd:element minOccurs="0" name="text" type="xsd:string"/></li> <li></xsd:sequence></li> <li></xsd:complexType></li> <li><xsd:element name="sayHiResponse" type="tns:sayHiResponse"/></li> <li><xsd:complexType name="sayHiResponse"></li> <li><xsd:sequence></li> <li><xsd:element minOccurs="0" name="return" type="xsd:string"/></li> <li></xsd:sequence></li> <li></xsd:complexType> ....</li> </ul>
5. Create a client to verify whether the web service server works.
The Web service server has been published, now we are going to write a piece of client program to call this service.
public class Client { private Client() { } public static void main(String[] args) { //create WebService client proxy factory JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); //register WebService interface factory.setServiceClass(HelloWorld.class); //set webservice publish address to factory. factory.setAddress("http://localhost:9000/HelloWorld"); IHelloWorld iHelloWorld = (IHelloWorld) factory.create(); System.out.println("invoke webservice..."); System.out.println("message context is:" + iHelloWorld.sayHi("Josen")); System.exit(0); } }
We can run the client main
method to remotely call the web service, after that, switch to console of web service server, should be able to see that a message sayHello is called by Josen
was printed.