Spring WS Client WSDL java

Spring WS Client WSDL java

Here you will see how to generate java classes using spring web service client from WSDL.

Below has been used:

  • Eclipse ( We are using eclipse Kepler download from here)
  • Tomcat 7.0 (Download from Apache site here)
  • Spring WS 2.1.3 (Note: All dependent jars are included in the project and you could download from the bottom link)

Note: We are using online free web service Stock Quote for this demo. You could go to this link and use Stock Quote WSDL for test.

Steps:

  • Create dynamic project in eclipse name: SpringWSDLClientGenaration
  • Below is final project structure:

Spring WS Client WSDL java

  • Create build.xml file inside project root folder and copy past below code:
<?xml version="1.0" encoding="UTF-8"?>

<project name="Generate Spring client from wsdl" default="" basedir=".">

    <description>Generate Spring client from wsdl</description>

    <property name="root" value="." />
    <property name="lib" location="${root}/WebContent/WEB-INF/lib" />

    <path id="classpathid">
        <fileset dir="${lib}" includes="*.jar" />
    </path>

    <!-- http://jaxb.java.net/2.2.6/docs/ch04.html#tools-xjc-ant-task -->
    <taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
        <classpath refid="classpathid" />
    </taskdef>

    <!--    Generate Targets for Accessibility Fee service -->
    <target name="generate.wsdl" description="- -> Generate XML binding artifacts">
        <!-- http://jaxb.java.net/2.2.6/docs/ch04.html#tools-xjc-ant-task -->
        <xjc destdir="${basedir}/src" package="generated.webClient.stock" removeOldOutput="yes">
            <arg value="-verbose" />
            <arg value="-wsdl" />
            <arg value="${basedir}/conf/stockquote.wsdl" />
        </xjc>
    </target>

</project>
  • Copy below jars inise WEB-INF/lib folder:

Spring WS Client WSDL java

  • Create conf folder inside project root folder and copy paste stockquote.wsdl from this URL
  • Now ready to generate java class using Spring WS form WSDL
  • To generate classes: Right click –> build.xml –> Run As –> Ant Build…

Spring WS Client WSDL java

  • Click Run:

Spring WS Client WSDL java

  • Once you see BUILD SUCCESSFUL message on console, refresh your project you will package=”generated.webClient.stock” got created

Spring WS Client WSDL java

  • Create test class TestStockWebService.java inside com.javahonk package to validate web service:
package com.javahonk;


import generated.webClient.stock.GetQuote;
import generated.webClient.stock.GetQuoteResponse;

import java.io.IOException;

import javax.xml.bind.JAXBElement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support
                .ClassPathXmlApplicationContext;
import org.springframework.ws.client.core.WebServiceTemplate;


public class TestStockWebService {
    
    @Autowired
    private static WebServiceTemplate larsFeeTemplate;

    public static void main(String[] args) throws IOException {
        
        ClassPathXmlApplicationContext applicationContext = 
                new ClassPathXmlApplicationContext(
                        "applicationContext.xml");
        
        WebServiceTemplate stockTemplate = (WebServiceTemplate)
                applicationContext.getBean("stockTemplate");
        
        
        GetQuote serviceRequest = new generated.webClient
                .stock.ObjectFactory().createGetQuote();
        serviceRequest.setSymbol("c");
        JAXBElement<GetQuoteResponse> responseObject =  
                (JAXBElement<GetQuoteResponse>) stockTemplate
                .marshalSendAndReceive(serviceRequest);
        
        System.out.println("Name: "+responseObject.getName()
                +" Value: "+responseObject.getValue());
        
        applicationContext.close();

    }

}

  • To run –> Right click TestStockWebService.java –> Run As –> Java Application. You will see stock name City bank “c” name and value printed on console.

download Download Project: SpringWSDLClientGenaration

That’s it Spring WS Client WSDL java

Leave a Reply

Your email address will not be published. Required fields are marked *