Web Service Development Using CXF
Total Page:16
File Type:pdf, Size:1020Kb
Web Service Development Using CXF - Praveen Kumar Jayaram Introduction to WS Web Service define a standard way of integrating systems using XML, SOAP, WSDL and UDDI open standards over an internet protocol backbone (HTTP). XML – Tags the data (Extensible Markup Language) SOAP – Used to transfer data (Simple Object Access Protocol) WSDL – Describes services available (Web Service Definition Language) UDDI – Lists the services available in directory (Universal Description Discovery Integration) Why Web Service? High interoperability. Web services are not tied to any programming language or operating system Web Service Frameworks: Axis CXF – We are interested in CXF in this session CXF – Web Service Framework Apache CXF is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. Why CXF? 1) JAX-WS support (Frontend) - Ease of building services - Generating WSDL from java classes and java classes from WSDL 2) Spring integration for declaring service endpoints 3) Aegis data binding - Unlike JAXB Aegis does not require annotation for building services 4) Apache liberal licensed and can be used for any type of applications Advantages of CXF over Axis: 1) CXF is JAX-WS complaint whereas Axis falls into proprietary things 2) CXF is very active in fix packs, releases and committers respond to issues often 3) CXF has better support for Spring integration 4) CXF is bit faster than Axis 1 (almost same for Axis 2) and easier to use http://cxf.apache.org/docs/index.html Creating a service using CXF Create a service contract interface using annotations: @WebService(serviceName=“HelloWorldService”, targetNamespace=“http://helloworld.com”) public interface IHelloWorld{ @WebMethod public String sayHello(@WebParam(name=“name”) String name); } JAX-WS includes the annotations: 1) WebService – Allows to customize the service name, target namespace and port 2) WebMethod – Allows to customize the operation name 3) WebResult – Allows to customize return value of the web service call 4) WebParam – Helps in giving a name for parameter (Clear for service consumers) Using JAX-WS annotated services Implement the service method sayHello(), package com.honeywell.ws.demo; public class HellowWorldImpl implements IHelloWorld{ public void sayHello(String name){ return “Hello ” + name; } } As we are aware CXF has better integration with Spring, we shall create service endpoint using configurations. Lets create a spring configuration file by name appContext.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <jaxws:endpoint implementor="com.honeywell.ws.demo.HelloWorldImpl" address=“/helloworld"/> </beans> Using JAX-WS annotated services Finally we will get the appContext.xml included in web.xml file as a context parameter. <?xml version="1.0"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Hello World</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/appContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping> </web-app> Using JAX-WS annotated services Now we have service up and running in Tomcat. The below link should allow you to access the WSDL, http://localhost:8080/HelloWorldApp/services/helloworld?wsdl You can find an example explaining the steps to create a service and deploying in Tomcat http://www.benmccann.com/dev-blog/web-services-tutorial-with-apache-cxf/ Creating a client to consume WS A web service client can be created in following ways, • Using wsdl2Java command/Soap UI tool • JAX-WS Proxy • Using dynamic client Using wsdl2java command: wsdl2java –client –p com.honeywell.ws.client http://localhost:8080/HelloWorldApp/services/helloworld?wsdl –d D:/HelloWorldClient The above command generates binding classes for the service. Let say it had generated a class HelloWorldImplService as the main class. Get the client instance from the HelloWorldImplService class to interact with the service, HelloWorldImplService service = new HelloWorldImplService(); IHelloWorld client = service.getHelloWorldImplPort(); String str = client.sayHello(“CXF developers!”); Output: Hello CXF developers! Generating client using SoapUI - freeware Developers can relax after this section… We have SoapUI tool for generating Axis or CXF clients to call a service. Steps to load a WSDL, test it online (tool generates a client internally) and generate client for external use. 1) Create a new project and input the WSDL which needs to be tested Generating client using SoapUI - freeware 2) Test the service by providing the input, Generating client using SoapUI - freeware 3) Generate the client by checking the “Client” option. Generating a proxy/dynamic client Traditional way of creating a proxy class to call a webservice, URL wsdlURL = new URL("http://localhost:8080/HelloworldApp/services/helloworld?wsdl"); QName SERVICE_NAME = new QName("http://demo.ws.honeywell.com", “HelloworldImplService"); Service service = Service.create(wsdlURL, SERVICE_NAME); IHelloWorld client = service.getPort(IHelloWorld.class); String result = client.sayHello(“CXF developers!"); System.out.println(“sayHello() response: ” + result); Creating a dynamic client to call a webservice, JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient(" http://localhost:8080/HelloworldApp/services/helloworld?wsdl"); Object[] res = client.invoke(“sayHello", “CXF developers!"); System.out.println(“sayHello() response: " + res[0]); Further reading… Web Service Security: http://cxf.apache.org/docs/ws-security.html RESTful Service: http://cxf.apache.org/docs/jax-rs.html Sample resources attached: HelloWorldApp.war HelloWorldClient.rar.