Thursday, March 6, 2014

How to create Spring based web service client with Apache CXF

In previous article, i have explained how to develop the Web Service using the Apache CXF framework. today i am planning to demonstrate on developing spring based web service client using Apache CXF.


In here, i will be developing a web service client for the above web service. you may follow the following steps.

first of all, you need to make sure that your web service is up and running.
if so, you can follow the steps given below to create your spring based web service client.


1. first create a maven based project. i have created a maven based simple java application.


2. Then add the spring dependency for the pom.xml

3. Then add the Apache CXF dependencies for the pom.xml


eventually your pom.xml will looks as below.

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.chathurangaonline.apache.cxf.jaxws.spring.samples</groupId>
    <artifactId>apache-cxf-jaxws-spring-jaxws-spring-client</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>apache-cxf-jaxws-spring-jaxws-spring-client Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <cxf.version>3.0.4</cxf.version>
        <spring.version>4.0.2.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!--spring dependencies-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!--apache cxf dependencies-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>apache-cxf-jaxws-spring-jaxws-spring-client</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>




4. Now generate the client side libraries/stubs that required for the web service client from the WSDL of the service. then integrate the generated client stub files with your client application. this can be done with the following command.
if you need more information, you can refer this article for it.

wsimport  -keep  -verbose -d /home/chathuranga/Projects/jax-ws-tutorial/jax-ws-jaxb-sample-app/webClient/jaxb-client/src/main/java/  http://localhost:8080/employee-service/empServiceUrl?wsdl


5. create a applicationContext.xml file in the resource folder and add the following configurations there.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:context="http://www.springframework.org/schema/context"
       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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <context:property-placeholder location="classpath*:configuration.properties"/>

    <jaxws:client id="calcServiceBean"
                  serviceClass="com.chathurangaonline.apache.cxf.jaxws.spring.samples.impl.CalculatorService"
                  address="${web.service.multiplication.url}" />


</beans>


6. create the configuration.properties file in resources directory and add the following entry to it.

 web.service.multiplication.url = http://localhost:8080/apache-cxf-jaxws-spring-sample/calcService


7. now you can create the client as follows.

package com.chathurangaonline.apache.cxf.jaxws.client.samples;

import com.chathurangaonline.apache.cxf.jaxws.spring.samples.impl.CalculatorService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CXFTestClient {

    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
        CalculatorService calculatorService = (CalculatorService) applicationContext.getBean("calcServiceBean");
        double answer =  calculatorService.multiply(10,22);
        System.out.println(" answer is ["+answer+"]");
    }
}



you can download the fully source code from the following repository.

Source Code :- https://github.com/chathurangat/apache-cxf-jaxws-spring-client

Hope this will be helpful for you!

Thanks
Chathuranga Tennakoon
www.chathurangaonline.com


No comments:

Post a Comment