<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web Service &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/web-service/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 17:20:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>Web Service &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Using Spring Web Services and JAXB to Invoke Web Service Based on WSDL</title>
		<link>https://myshittycode.com/2013/10/01/using-spring-web-services-and-jaxb-to-invoke-web-service-based-on-wsdl/</link>
					<comments>https://myshittycode.com/2013/10/01/using-spring-web-services-and-jaxb-to-invoke-web-service-based-on-wsdl/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Tue, 01 Oct 2013 13:17:11 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JAXB 2]]></category>
		<category><![CDATA[JAXB-2 Maven Plugin]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring Web Services]]></category>
		<category><![CDATA[Web Service]]></category>
		<category><![CDATA[WSDL]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=80</guid>

					<description><![CDATA[<p>There are several ways to consume a web service based on a WSDL from Java. After trying a couple of approaches, I&#8217;m currently leaning towards Spring Web Services and JAXB. The biggest advantage of using both Spring Web Services and JAXB to consume a web service is the flexibility to change the web service URL [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2013/10/01/using-spring-web-services-and-jaxb-to-invoke-web-service-based-on-wsdl/">Using Spring Web Services and JAXB to Invoke Web Service Based on WSDL</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">There are several ways to consume a web service based on a WSDL from Java. After trying a couple of approaches, I&#8217;m currently leaning towards Spring Web Services and JAXB. The biggest advantage of using both Spring Web Services and JAXB to consume a web service is the flexibility to change the web service URL without the need to regenerate the needed Java files, especially if you have a different web service URL for each environment (DEV, TEST and PROD).</p>



<p class="wp-block-paragraph">In this tutorial, I&#8217;m going to consume this freely available web service that allows me to convert the currency rate from one country to another: http://www.webservicex.net/CurrencyConvertor.asmx?WSDL</p>



<h2 class="wp-block-heading">currency.wsdl <b>( src/main/resources )</b></h2>



<p class="wp-block-paragraph">The first step is to open the WSDL link on the browser and save a copy of it. In this example, I&#8217;m saving it as <b>currency.wsdl</b>.</p>



<p class="wp-block-paragraph">Your project structure should look something like this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
myproject
|- src
|  |- main
|     |- java
|     |  |- myproject
|     |- resources
|        |- currency.wsdl
|- pom.xml
</pre></div>


<h2 class="wp-block-heading">pom.xml</h2>



<p class="wp-block-paragraph">Add Spring Web Services dependency and configure JAXB-2 Maven plugin to generate the Java files based on the WSDL.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;project&gt;
	...
	&lt;dependencies&gt;
	    &lt;dependency&gt;
	        &lt;groupid&gt;org.springframework.ws&lt;/groupid&gt;
	        &lt;artifactid&gt;spring-ws-core&lt;/artifactid&gt;
	        &lt;version&gt;2.1.2.RELEASE&lt;/version&gt;
	    &lt;/dependency&gt;
	&lt;/dependencies&gt;

    &lt;build&gt;
        &lt;plugins&gt;
			...

            &lt;plugin&gt;
                &lt;groupid&gt;org.codehaus.mojo&lt;/groupid&gt;
                &lt;artifactid&gt;jaxb2-maven-plugin&lt;/artifactid&gt;
                &lt;version&gt;1.5&lt;/version&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;id&gt;xjc&lt;/id&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;xjc&lt;/goal&gt;
                        &lt;/goals&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
                &lt;configuration&gt;
					&lt;!-- Package to store the generated file --&gt;
                    &lt;packagename&gt;myproject.wsdl.currency&lt;/packagename&gt;
					&lt;!-- Treat the input as WSDL --&gt;
                    &lt;wsdl&gt;true&lt;/wsdl&gt;
                    &lt;!-- Input is not XML schema --&gt;
					&lt;xmlschema&gt;false&lt;/xmlschema&gt;
                    &lt;!-- The WSDL file that you saved earlier --&gt;
                    &lt;schemafiles&gt;currency.wsdl&lt;/schemafiles&gt;
                    &lt;!-- The location of the WSDL file --&gt;
					&lt;schemadirectory&gt;${project.basedir}/src/main/resources&lt;/schemadirectory&gt;
                    &lt;!-- The output directory to store the generated Java files --&gt;
					&lt;outputdirectory&gt;${project.basedir}/src/main/java&lt;/outputdirectory&gt;
                    &lt;!-- Don&#039;t clear output directory on each run --&gt;
                    &lt;clearoutputdir&gt;false&lt;/clearoutputdir&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;
</pre></div>


<p class="wp-block-paragraph">Setting <b>clearOutputDir</b> to <b>false</b> is very important. If this is set to <b>true</b> and there&#8217;s a silly error in the JAXB-2 Maven plugin configuration, your output directory will be wiped clean. Since the output directory points to <b>${project.basedir}/src/main/java</b>, we want to make sure we don&#8217;t lose all of our existing Java files.</p>



<h2 class="wp-block-heading">Generating Java Files Based on WSDL</h2>



<p class="wp-block-paragraph">While you can run <b>mvn clean jaxb2:xjc</b> to generate the Java files, the easier way is to just run <b>mvn clean compile</b>. You should see a similar Maven output in your console:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&#x5B;INFO] ------------------------------------------------------------------------
&#x5B;INFO] Building myproject 1.0
&#x5B;INFO] ------------------------------------------------------------------------
&#x5B;INFO]
&#x5B;INFO] --- jaxb2-maven-plugin:1.5:xjc (xjc) @ myproject ---
&#x5B;INFO] Generating source...
&#x5B;INFO] parsing a schema...
&#x5B;INFO] compiling a schema...
&#x5B;INFO] myproject/wsdl/currency/ConversionRate.java
&#x5B;INFO] myproject/wsdl/currency/ConversionRateResponse.java
&#x5B;INFO] myproject/wsdl/currency/Currency.java
&#x5B;INFO] myproject/wsdl/currency/ObjectFactory.java
&#x5B;INFO] myproject/wsdl/currency/package-info.java
&#x5B;INFO]
</pre></div>


<p class="wp-block-paragraph">Your project structure should look something like this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
myproject
|- src
|  |- main
|     |- java
|     |  |- myproject
|     |     |- wsdl
|     |        |- currency
|     |           |- ConversionRate.java
|     |           |- ConversionRateResponse.java
|     |           |- Currency.java
|     |           |- ObjectFactory.java
|     |           |- package-info.java
|     |- resources
|        |- currency.wsdl
|- pom.xml
</pre></div>


<h2 class="wp-block-heading">applicationContext.xml <b>( src/main/resources )</b></h2>



<p class="wp-block-paragraph">Once we have the generated Java files, the next step is to configure Spring Web Services.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?--&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:context=&quot;http://www.springframework.org/schema/context&quot; xmlns:oxm=&quot;http://www.springframework.org/schema/oxm&quot; xmlns:util=&quot;http://www.springframework.org/schema/util&quot; xsi:schemalocation=&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd&quot;&gt;

    &lt;context:component-scan base-package=&quot;myproject&quot;&gt;

	&lt;!-- Define the SOAP version used by the WSDL --&gt;
    &lt;bean id=&quot;soapMessageFactory&quot; class=&quot;org.springframework.ws.soap.saaj.SaajSoapMessageFactory&quot;&gt;
        &lt;property name=&quot;soapVersion&quot;&gt;
            &lt;util:constant static-field=&quot;org.springframework.ws.soap.SoapVersion.SOAP_12&quot;&gt;
        &lt;/util:constant&gt;&lt;/property&gt;
    &lt;/bean&gt;

	&lt;!-- The location of the generated Java files --&gt;
    &lt;oxm:jaxb2-marshaller id=&quot;marshaller&quot; contextpath=&quot;myproject.wsdl.currency&quot;&gt;

	&lt;!-- Configure Spring Web Services --&gt;
    &lt;bean id=&quot;webServiceTemplate&quot; class=&quot;org.springframework.ws.client.core.WebServiceTemplate&quot;&gt;
        &lt;constructor-arg ref=&quot;soapMessageFactory&quot;&gt;
        &lt;property name=&quot;marshaller&quot; ref=&quot;marshaller&quot;&gt;
        &lt;property name=&quot;unmarshaller&quot; ref=&quot;marshaller&quot;&gt;
        &lt;property name=&quot;defaultUri&quot; value=&quot;http://www.webservicex.net/CurrencyConvertor.asmx?WSDL&quot;&gt;
    &lt;/property&gt;&lt;/property&gt;&lt;/property&gt;&lt;/constructor-arg&gt;&lt;/bean&gt;
&lt;/oxm:jaxb2-marshaller&gt;&lt;/context:component-scan&gt;&lt;/beans&gt;
</pre></div>


<h2 class="wp-block-heading">CurrentService.java <b>( src/main/java/myproject/service )</b></h2>



<p class="wp-block-paragraph">In the Service class, we will use Spring Web Services <b>WebServiceTemplate</b> to call the web service.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
@Service
public class CurrencyService {
    @Autowired
    private WebServiceTemplate webServiceTemplate;

    public Double getConversionRate(Currency fromCurrency, Currency toCurrency) {
        ConversionRate conversionRate = new ObjectFactory().createConversionRate();
        conversionRate.setFromCurrency(fromCurrency);
        conversionRate.setToCurrency(toCurrency);

        ConversionRateResponse response = (ConversionRateResponse) webServiceTemplate.marshalSendAndReceive(
                conversionRate);

        return response.getConversionRateResult();
    }
}
</pre></div>


<h2 class="wp-block-heading">Main.java <b>( src/main/java/myproject/main )</b></h2>



<p class="wp-block-paragraph">This is the main runner class.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
public class Main {
    private static Logger log = Logger.getLogger(Main.class);

    public static void main(String&#x5B;] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;);
        CurrencyService currencyService = context.getBean(CurrencyService.class);

        Currency fromCurrency = Currency.USD;
        Currency toCurrency = Currency.GBP;
        Double conversionRate = currencyService.getConversionRate(fromCurrency, toCurrency);

        log.info(String.format(&quot;The conversion rate from %s to %s is %s.&quot;, fromCurrency, toCurrency, conversionRate));
    }
}
</pre></div>


<h2 class="wp-block-heading">Project Structure</h2>



<p class="wp-block-paragraph">Your project structure should look something like this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
myproject
|- src
|  |- main
|     |- java
|     |  |- myproject
|     |     |- main
|     |        |- Main.java
|     |     |- service
|     |        |- CurrencyService.java
|     |     |- wsdl
|     |        |- currency
|     |           |- ConversionRate.java
|     |           |- ConversionRateResponse.java
|     |           |- Currency.java
|     |           |- ObjectFactory.java
|     |           |- package-info.java
|     |- resources
|        |- currency.wsdl
|- pom.xml
</pre></div>


<h2 class="wp-block-heading">Result</h2>



<p class="wp-block-paragraph">When you run <b>Main.java</b>, you should see a result similar to this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
The conversion rate from USD to GBP is 0.6178.
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2013/10/01/using-spring-web-services-and-jaxb-to-invoke-web-service-based-on-wsdl/">Using Spring Web Services and JAXB to Invoke Web Service Based on WSDL</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2013/10/01/using-spring-web-services-and-jaxb-to-invoke-web-service-based-on-wsdl/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">80</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk

Served from: myshittycode.com @ 2026-07-05 10:22:38 by W3 Total Cache
-->