Home > Web Services > How to create SOAP web service client in java with cxf, Spring, Maven

How to create SOAP web service client in java with cxf, Spring, Maven

July 9, 2012

In continuation with my previous post i am showing a way how to invoke a SOAP service from java.

There are different ways to create a SOAP Clinet in java with cxf.Here im giving the most fastest and easiest way to create.

Get the wsdl URL form the server.

open up the pom.xml file.

Add the below wsdl-to-java plugin.This plugin will generate the java code by parsin the wsdl which is given as input to it.

<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>generated/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>http://localhost:7009/cxf-spring-test/calculatorService?wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>

now open the commanprompt or terminal,navigate to the project

type mvn clean generate-sources

this will generate the wsdl correspondin code in the <Project Folder>/generated/cxf location.

Add this to the project build path.

now i am writing a simple class and wired to the bean in the application context

Application Context File:

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" 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://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<!-- Start up bean which will invoke server webservice -->
<bean id="calculatorService" init-method="invoke">
<constructor-arg index="0" value="http://localhost:7009/cxf-spring-test/calculatorService?wsdl"/>
</bean>

</beans>

Java Client Class(As per the above example this will get invoked automatically during start up)

package com.test;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class CalculatorServiceClient
{
private String targetURL;

public CalculatorServiceClient(String targetURL)
{
this.targetURL = targetURL;
}

public void invoke()
{
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(CalculatorIntf.class);

factory.setAddress(this.targetURL);
CalculatorIntf calculatorIntf = (CalculatorIntf) factory.create();

//invoking add web servcie
System.out.println("10 + 15 = "+calculatorIntf.add(10, 15));

//invoking subtract web service
System.out.println("15 - 5 = "+calculatorIntf.subtract(15, 5));

//invoking multiply web service
System.out.println("10 * 5 = "+calculatorIntf.multiply(10,5));
}
}

In the next post i will show how to add SSL Support to the above client code.

Source Code Download : cxf_spring_maven.zip

Advertisements
Categories: Web Services
%d bloggers like this: