<?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>JAXB 2 &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/jaxb-2/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=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>JAXB 2 &#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>JAXB2: Adding toString() to Generated Java Classes</title>
		<link>https://myshittycode.com/2017/03/07/jaxb2-adding-tostring-to-generated-java-classes/</link>
					<comments>https://myshittycode.com/2017/03/07/jaxb2-adding-tostring-to-generated-java-classes/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Tue, 07 Mar 2017 16:09:09 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JAXB 2]]></category>
		<category><![CDATA[JAXB-2 Maven Plugin]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=915</guid>

					<description><![CDATA[<p>PROBLEM By default, the generated Java class prints the memory address when toString() is invoked. However, sometimes it is helpful to have a more meaningful toString() for debugging purposes. SOLUTION To fix this, configure maven-jaxb2-plugin to generate toString() based on the fields in the class:-</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2017/03/07/jaxb2-adding-tostring-to-generated-java-classes/">JAXB2: Adding toString() to Generated Java Classes</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>By default, the generated Java class prints the memory address when <b>toString()</b> is invoked.</p>



<p>However, sometimes it is helpful to have a more meaningful <b>toString()</b> for debugging purposes.</p>



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



<p>To fix this, configure <b>maven-jaxb2-plugin</b> to generate <b>toString()</b> based on the fields in the class:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; highlight: [5,9,10,11,12,13,46,47,48,49,50,51,52,53,54,55]; title: ; notranslate">
&lt;project ...=&quot;&quot;&gt;
  ...

  &lt;properties&gt;
    &lt;org.jvnet.jaxb2_commons.version&gt;0.11.1&lt;/org.jvnet.jaxb2_commons.version&gt;
  &lt;/properties&gt;

  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupid&gt;org.jvnet.jaxb2_commons&lt;/groupid&gt;
      &lt;artifactid&gt;jaxb2-basics-runtime&lt;/artifactid&gt;
      &lt;version&gt;${org.jvnet.jaxb2_commons.version}&lt;/version&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;

  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;groupid&gt;org.jvnet.jaxb2.maven2&lt;/groupid&gt;
        &lt;artifactid&gt;maven-jaxb2-plugin&lt;/artifactid&gt;
        &lt;version&gt;0.13.1&lt;/version&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;goals&gt;
              &lt;goal&gt;generate&lt;/goal&gt;
            &lt;/goals&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
        &lt;configuration&gt;
          &lt;schemalanguage&gt;WSDL&lt;/schemalanguage&gt;
          &lt;generatepackage&gt;my.package.wsdl&lt;/generatepackage&gt;
          &lt;bindingdirectory&gt;${project.basedir}/src/main/resources&lt;/bindingdirectory&gt;
          &lt;bindingincludes&gt;
            &lt;include&gt;jaxb-binding.xjb&lt;/include&gt;
          &lt;/bindingincludes&gt;
          &lt;schemas&gt;
            &lt;schema&gt;
              &lt;fileset&gt;
                &lt;directory&gt;${project.basedir}/src/main/resources&lt;/directory&gt;
                &lt;includes&gt;
                  &lt;include&gt;web-service.wsdl&lt;/include&gt;
                &lt;/includes&gt;
              &lt;/fileset&gt;
            &lt;/schema&gt;
          &lt;/schemas&gt;
          &lt;args&gt;
            &lt;arg&gt;-XtoString&lt;/arg&gt;
          &lt;/args&gt;
          &lt;plugins&gt;
            &lt;plugin&gt;
              &lt;groupid&gt;org.jvnet.jaxb2_commons&lt;/groupid&gt;
              &lt;artifactid&gt;jaxb2-basics&lt;/artifactid&gt;
              &lt;version&gt;${org.jvnet.jaxb2_commons.version}&lt;/version&gt;
            &lt;/plugin&gt;
          &lt;/plugins&gt;
        &lt;/configuration&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;
&lt;/project&gt;
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2017/03/07/jaxb2-adding-tostring-to-generated-java-classes/">JAXB2: Adding toString() to Generated Java Classes</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2017/03/07/jaxb2-adding-tostring-to-generated-java-classes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">915</post-id>	</item>
		<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>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>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>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>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>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>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>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>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>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>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>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>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>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-02-20 15:16:46 by W3 Total Cache
-->