Sparkles

that were shone when I got tempered!

How to send a SOAP Request using CXF JAX-WS

leave a comment »

1.Create a Maven Project
1

 

2. WSDL setups

Download the WSDL file and put to the resources folder (/src/main/resources/). You can use the wsdl URL also without downloading.
Sample WSDL : http://www.webservicex.net/stockquote.asmx?WSDL

 

3. pom.xml configuration

Properties :

<properties>
	<cxf.version>2.6.1</cxf.version>
</properties>	

Dependencies :

<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-core</artifactId>
	<version>${cxf.version}</version>
</dependency>
<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>

cxf-codegen-plugin configuration

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-codegen-plugin</artifactId>
			<version>${cxf.version}</version>
			<executions>
				<execution>
					<id>StockQuote</id>
					<phase>generate-sources</phase>
					<configuration>
						<sourceRoot>${basedir}/target/generated/src/main/java</sourceRoot>
						<wsdlOptions>
							<wsdlOption>
								<wsdl>${basedir}/src/main/resources/stockquote.wsdl</wsdl>
							</wsdlOption>
						</wsdlOptions>
					</configuration>
					<goals>
						<goal>wsdl2java</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

You can use the WSDL URL also instead of ${basedir}/src/main/resources/stockquote.wsdl.

Comment this cxf-codegen-plugin configuration part after you generated the code.

4. Generate the wsdl2java code.

Run pom.xml with clean install goals. This will generate the source for the WSDL in the location mentioned in (eg:${basedir}/target/generated/src/main/java)

Copy and Paste the generated packages to src/main/java folder.

2

5. Develop the service

Find the Adddress
This is mentioned in wsdl:service/wsdl:port/soap:address@location. (eg:http://www.webservicex.net/stockquote.asmx)

Find the ServiceClass.
This is mentioned in the wsdl:service/wsdl:port/@name (eg: StockQuoteSoap)

eg :

<wsdl:service name="StockQuote">
	<wsdl:port name="StockQuoteSoap" binding="tns:StockQuoteSoap">
  		<soap:address location="http://www.webservicex.net/stockquote.asmx" />
	</wsdl:port>
</wsdl:service>

Create client using address and the ServiceClass

protected StockQuoteSoap createCXFClient() {
    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    factory.setServiceClass(StockQuoteSoap.class);
    factory.setAddress("http://www.webservicex.net/stockquote.asmx");
    return (StockQuoteSoap) factory.create();
}

Find the Operation (meethod name), Requeest parameter(s) and Response type using the wsdl

<wsdl:portType name="StockQuoteSoap">
	<wsdl:operation name="GetQuote">
	  <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Get Stock quote for a company Symbol</wsdl:documentation>
	  <wsdl:input message="tns:GetQuoteSoapIn" />
	  <wsdl:output message="tns:GetQuoteSoapOut" />
	</wsdl:operation>
</wsdl:portType>

<wsdl:message name="GetQuoteSoapIn">
	<wsdl:part name="parameters" element="tns:GetQuote" />
</wsdl:message>

<wsdl:message name="GetQuoteSoapOut">
	<wsdl:part name="parameters" element="tns:GetQuoteResponse" />
</wsdl:message>
  • Method name : wsdl:portType/wsdl:operation/@name relevanet to the wsdl:service/wsdl:port/@binding
  • Requeest Parameter(s) : wsdl:message/wsdl:part/@element relevant to wsdl:portType/wsdl:operation/wsdl:input@name
  • Response Type : wsdl:message/wsdl:part/@element relevant to wsdl:portType/wsdl:operation/wsdl:output@name

Invoke the operation (method) using the created client

private void invokeJaxWs() {
	StockQuoteSoap client = createCXFClient();
	String response = client.getQuote("test123");
	System.out.println(response);
}

DONE!

GIT URL : <demo-cxf-jax-ws.git>

Advertisements

Written by Namal Fernando

August 2, 2016 at 7:32 am

Posted in JAX-WS, WebServices

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Ruth's Reflections

Contemplations from quakey quirky Christchurch

TED Blog

The TED Blog shares interesting news about TED, TED Talks video, the TED Prize and more.

Ziplok

Learn and discover simple things

Meihta Dwiguna Saputra's Knowledge Base

~In learning you will teach and in teaching you will (re)learn~

The Java Blog

Thoughts, tips and tricks about the Java programming language

Sparkles

that were shone when I got tempered!

%d bloggers like this: