<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Jamesbnuzzo's Blog</title>
	<atom:link href="http://jamesbnuzzo.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jamesbnuzzo.wordpress.com</link>
	<description>SOA, Spring and Apps Dev Management</description>
	<lastBuildDate>Fri, 02 Dec 2011 10:22:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='jamesbnuzzo.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Jamesbnuzzo's Blog</title>
		<link>http://jamesbnuzzo.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jamesbnuzzo.wordpress.com/osd.xml" title="Jamesbnuzzo&#039;s Blog" />
	<atom:link rel='hub' href='http://jamesbnuzzo.wordpress.com/?pushpress=hub'/>
		<item>
		<title>JAXBElement types generated when schema contain minOccurs=&#8221;0&#8243; and nillable=&#8221;true&#8221;</title>
		<link>http://jamesbnuzzo.wordpress.com/2009/11/01/jaxbelement-types-generated-when-schema-contain-minoccurs0-and-nillabletrue/</link>
		<comments>http://jamesbnuzzo.wordpress.com/2009/11/01/jaxbelement-types-generated-when-schema-contain-minoccurs0-and-nillabletrue/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 22:17:29 +0000</pubDate>
		<dc:creator>cyclocrossboy</dc:creator>
				<category><![CDATA[Java data types]]></category>
		<category><![CDATA[JAXB 2]]></category>
		<category><![CDATA[JAXBElement]]></category>
		<category><![CDATA[JAXB]]></category>
		<category><![CDATA[nilliable="true"]]></category>
		<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[SFDC]]></category>
		<category><![CDATA[wsdl]]></category>

		<guid isPermaLink="false">http://jamesbnuzzo.wordpress.com/?p=182</guid>
		<description><![CDATA[Problem JAXB is great for generating JAVA code necessary to support marshalling and unmarshalling of XML to Java Pojo&#8217;s particularly when working with web services. However with some WSDL&#8217;s the Java code generated contain JAXBElement types instead of Java data types. For example a schema fragment from a Salesforce notification wsdl: would generate the following [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesbnuzzo.wordpress.com&amp;blog=6412760&amp;post=182&amp;subd=jamesbnuzzo&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Problem</h2>
<p>JAXB is great for generating JAVA code necessary to support marshalling and unmarshalling of XML to Java Pojo&#8217;s particularly when working with web services.  However with some WSDL&#8217;s the Java code generated contain JAXBElement types instead of Java data types. For example a schema fragment from a Salesforce notification wsdl:</p>
<p><pre class="brush: xml;">
        &lt;schema elementFormDefault=&quot;qualified&quot; xmlns=&quot;http://www.w3.org/2001/XMLSchema&quot; targetNamespace=&quot;urn:sobject.enterprise.soap.sforce.com&quot;&gt;
            &lt;import namespace=&quot;urn:enterprise.soap.sforce.com&quot;/&gt;
            &lt;!-- Base sObject (abstract) --&gt;
            &lt;complexType name=&quot;sObject&quot;&gt;
                &lt;sequence&gt;
                    &lt;element name=&quot;fieldsToNull&quot; type=&quot;xsd:string&quot; nillable=&quot;true&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot;/&gt;
                    &lt;element name=&quot;Id&quot; type=&quot;ent:ID&quot; nillable=&quot;true&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot;/&gt;
                &lt;/sequence&gt;
            &lt;/complexType&gt;
            &lt;complexType name=&quot;Opportunity&quot;&gt;
                &lt;complexContent&gt;
                    &lt;extension base=&quot;ens:sObject&quot;&gt;
                        &lt;sequence&gt;
                        &lt;element name=&quot;Sales_Opportunity_Number__c&quot; nillable=&quot;true&quot; minOccurs=&quot;0&quot; type=&quot;xsd:string&quot;/&gt;
                        &lt;/sequence&gt;
                    &lt;/extension&gt;
                &lt;/complexContent&gt;
            &lt;/complexType&gt;
        &lt;/schema&gt;
</pre></p>
<p>would generate the following corresponding Java code fragments:</p>
<p><pre class="brush: java;">
public class Opportunity
    extends SObject
{

    @XmlElementRef(name = &quot;Sales_Opportunity_Number__c&quot;, namespace = &quot;urn:sobject.enterprise.soap.sforce.com&quot;, type = JAXBElement.class)
    protected JAXBElement&lt;String&gt; salesOpportunityNumberC;

    /**
     * Gets the value of the salesOpportunityNumberC property.
     *
     * @return
     *     possible object is
     *     {@link JAXBElement }{@code &lt;}{@link String }{@code &gt;}
     *
     */
    public JAXBElement&lt;String&gt; getSalesOpportunityNumberC() {
        return salesOpportunityNumberC;
    }

    /**
     * Sets the value of the salesOpportunityNumberC property.
     *
     * @param value
     *     allowed object is
     *     {@link JAXBElement }{@code &lt;}{@link String }{@code &gt;}
     *
     */
    public void setSalesOpportunityNumberC(JAXBElement&lt;String&gt; value) {
        this.salesOpportunityNumberC = ((JAXBElement&lt;String&gt; ) value);
    }
}
</pre></p>
<p>The JAXBElementsalesOpportunityNumberC type is a bit cumbersome to work with your Java code.</p>
<h2>Solution</h2>
<p>A simple solution to this problem is to edit a <strong>copy</strong> of your wsdl. Do the following:</p>
<ul>
<li>Replace all <strong>nillable=”true”</strong> to <strong>nillable=&#8221;false&#8221;</strong>
</ul>
<p>The desired side effect of this generating Java code with the desired Java types in place.  See corresponding example:</p>
<p><pre class="brush: java;">
public class Opportunity
    extends SObject
{

    @XmlElement(name = &quot;Sales_Opportunity_Number__c&quot;, namespace = &quot;urn:sobject.enterprise.soap.sforce.com&quot;)
    protected String salesOpportunityNumberC;

    /**
     * Gets the value of the SalesOpportunityNumberC property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getSalesOpportunityNumberC() {
        return salesOpportunityNumberC;
    }

    /**
     * Sets the value of the salesOpportunityNumberC property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setSalesOpportunityNumberC(String value) {
        this.salesOpportunityNumberC = value;
    }

}
</pre></p>
<h2>Props</h2>
<p><a href="http://www.linkedin.com/profile?viewProfile=&amp;key=12388388">Brian Dussault</a> and <a href="http://www.linkedin.com/pub/julie-dougherty/6/214/2b3">Julie Dougherty</a> word on this problem and came up with the solution documented and currently used by my team.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jamesbnuzzo.wordpress.com/182/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jamesbnuzzo.wordpress.com/182/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jamesbnuzzo.wordpress.com/182/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jamesbnuzzo.wordpress.com/182/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jamesbnuzzo.wordpress.com/182/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jamesbnuzzo.wordpress.com/182/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jamesbnuzzo.wordpress.com/182/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jamesbnuzzo.wordpress.com/182/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jamesbnuzzo.wordpress.com/182/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jamesbnuzzo.wordpress.com/182/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jamesbnuzzo.wordpress.com/182/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jamesbnuzzo.wordpress.com/182/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jamesbnuzzo.wordpress.com/182/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jamesbnuzzo.wordpress.com/182/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesbnuzzo.wordpress.com&amp;blog=6412760&amp;post=182&amp;subd=jamesbnuzzo&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesbnuzzo.wordpress.com/2009/11/01/jaxbelement-types-generated-when-schema-contain-minoccurs0-and-nillabletrue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/71d5fa4384ecade10c6278c30e079094?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cyclocrossboy</media:title>
		</media:content>
	</item>
		<item>
		<title>Spring Web Service Client &#8211; EightBallDemo</title>
		<link>http://jamesbnuzzo.wordpress.com/2009/09/22/spring-web-service-client-eightballdemo/</link>
		<comments>http://jamesbnuzzo.wordpress.com/2009/09/22/spring-web-service-client-eightballdemo/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 23:58:11 +0000</pubDate>
		<dc:creator>cyclocrossboy</dc:creator>
				<category><![CDATA[Spring WS]]></category>
		<category><![CDATA[Spring WS Client]]></category>
		<category><![CDATA[Springframework]]></category>

		<guid isPermaLink="false">http://jamesbnuzzo.wordpress.com/?p=59</guid>
		<description><![CDATA[Introduction This is a followup to my previous blog on Spring Web Service’s with JAXB, Maven &#38; Eclipse and the Magic 8-Ball demo as a sample. Here we will use that sample web service and write a web service client that will call the Magic 8-Ball demo service. Again we will use Spring Web Services [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesbnuzzo.wordpress.com&amp;blog=6412760&amp;post=59&amp;subd=jamesbnuzzo&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>This is a followup to my previous blog on <a href="http://jamesbnuzzo.wordpress.com/">Spring Web Service’s with JAXB, Maven &amp; Eclipse</a> and the Magic 8-Ball demo as a sample.  Here we will use that sample web service and write a web service <strong>client</strong> that will call the Magic 8-Ball demo service.  Again we will use Spring Web Services to implement this web service client.</p>
<p>For Spring Web Services clients Spring continues to leverage its use of the Template pattern and provide &#8220;helper methods to perform common operations, and for more sophisticated usage, delegate to user implemented callback interfaces&#8230;various convenience methods for the sending and receiving of XML messages, marshalling objects to XML before sending, and allows for multiple transport options.&#8221; See <a href="http://static.springsource.org/spring-ws/sites/1.5/reference/html/client.html">Spring Web Service Client</a>.  As usual Spring provides the application infrastructure allowing you to focus on the core application logic: in our case calling the Magic 8-Ball web services and getting back the response.</p>
<p>We will also reuse some of the artifacts we used to build the Magic 8-Ball demo service; specifically the JAXB marshalling and unmarshalling class generated from the eightball.xsd which defined the data contract for the Magic 8-Ball demo service.</p>
<h2>Assumptions</h2>
<ul>
<li>You have an instance of the 8-Ball Web service running in Java container (i.e., Tomcat) and is accessible by URL:</li>
<blockquote><p><em> http://localhost:8080/eightball/ </em></p></blockquote>
</ul>
<p><strong>Tools User</strong><br />
We are using an updated tool set for this sample:</p>
<ul>
<li>Spring Tool Suite: <a href="http://www.springsource.com/products/sts/registration">sts-2.1.0.M1 (more recent versions exist)</a> and should be fine.</li>
<li><a href="https://jaxb.dev.java.net/">JAXB 2</a></li>
</ul>
<h2>Project</h2>
<p>In STS use File/New/Project…/Maven Project</p>
<ol>
<li>Select Default workspace</li>
<li>Select spring-ws-archtype</li>
<li>Enter client8ball for Group Id</li>
<li>Enter client8ball for Artifact Id</li>
<li>Select Finish</li>
<li>Selecting the POM.XML under the dependencies tab add dependencies in the Tools Used section above
<ul>
<li>Add spring-oxm-tiger dependency</li>
<li>Add spring-ws-core-tiger dependency</li>
</ul>
</li>
</ol>
<div id="attachment_71" class="wp-caption alignleft" style="width: 376px"><img class="size-full wp-image-71" title="dependency" src="http://jamesbnuzzo.files.wordpress.com/2009/06/dependency.gif?w=450" alt="POM Dependencies"   /><p class="wp-caption-text">POM Dependencies</p></div>
<h2>Leveraging the Contract</h2>
<h3>Using the JAXB Artifacts</h3>
<p>Spring Web Services implement the Contract First design pattern.  As part of the previous Magic 8-Ball example we used JAXB to generate the Java classes to support the marshalling and unmarshalling of our XML requests and response documents as specified in our XSD which defined our contract.  In this example we are going to leverage the same JAXB artifacts in support of the Spring Web Service client&#8217;s marshalling and unmarshalling.</p>
<ol>
<li>Create the package <strong>jaxb.eightball</strong> under the resource folder <strong>src/main/java</strong></li>
<li><strong>Import</strong> the JAXB Java files under the <strong>jaxb.eightball</strong> package.</li>
<p><img src="http://jamesbnuzzo.files.wordpress.com/2009/04/jaxbeightball.gif?w=450" alt="JAXB Artifacts" />
</ol>
<h2>Application Context</h2>
<p>Using Springs Inversion of Control (IoC) we are going to wire up our JAXB artifacts and soon to be written Web Service Client.</p>
<ol>
<li>In the resource folder <strong>src/main/resources/</strong> edit the file <strong>application-context.xml</strong></li>
</ol>
<p><pre class="brush: xml;">
&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;
       xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd&quot;&gt;

    &lt;bean id=&quot;eightBallClient&quot; class=&quot;client8ball.client8ball.EightBallClient&quot;&gt;
&lt;property name=&quot;defaultUri&quot; value=&quot;http://localhost:8080/eightball&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;/bean&gt;

     &lt;bean class=&quot;org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter&quot;&gt;
         &lt;constructor-arg ref=&quot;marshaller&quot; /&gt;
     &lt;/bean&gt;     

     &lt;bean id=&quot;marshaller&quot; class=&quot;org.springframework.oxm.jaxb.Jaxb2Marshaller&quot;&gt;
&lt;property name=&quot;classesToBeBound&quot;&gt;
	&lt;list&gt;
                &lt;value&gt;jaxb.eightball.EightBallRequest&lt;/value&gt;
                &lt;value&gt;jaxb.eightball.EightBallResponse&lt;/value&gt;
             &lt;/list&gt;
         &lt;/property&gt;
     &lt;/bean&gt;
&lt;/beans&gt;
</pre></p>
<ul>
<li>In the <strong>application-context.xml</strong> file we define our Magic 8-Ball client bean. <strong>eightballclient</strong>.  We inject the:
<ul>
<li> <strong>default uri, marshaller, unmarshaller</strong></li>
</ul>
</li>
<li>We define our end-point <strong>marshaller</strong> using the Spring provided class <strong>org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter</strong></li>
<li>We define our <strong>marshaller</strong> and the specific JAXB beans to be used:
<ul>
<li><strong>jaxb.eightball.EightBallRequest</strong></li>
<li><strong>jaxb.eightball.EightBallResponse</strong></li>
</ul>
</li>
</ul>
<h2>Magic 8-Ball Client Bean</h2>
<ol>
<li>In the resource folder <strong>src/main/java</strong> create a new package <strong>client8ball.client8ball</strong></li>
<li>In this package create a new class <strong>EightBallClient</strong> which extends <strong>WebServiceGatewaySupport</strong></li>
<li>Define the <strong>Request</strong> resource.</li>
<li>Define our method <strong>AskQuestion</strong> which will invoke our Magic 8-Ball Web Service and return its response.
<ul>
<li>Using a the webservicegateway method<br />
<pre class="brush: java;">
response =  (EightBallResponse)getWebServiceTemplate().marshalSendAndReceive(request);
</pre></p>
<p>we invoke the Magic 8-Ball service and return the response</li>
</ul>
</li>
</ol>
<p><pre class="brush: java;">
package client8ball.client8ball;

import java.io.IOException;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.core.io.Resource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import jaxb.eightball.EightBallRequest;
import jaxb.eightball.EightBallResponse;

public class EightBallClient extends WebServiceGatewaySupport {

	private Resource request;
	public void setRequest(Resource request){
		this.request = request;
	}
	public String AskQuestion(String question) throws IOException{
		String responseString = null;

		EightBallRequest request = new EightBallRequest();
		request.setQuestion(question);

		EightBallResponse response = new EightBallResponse();

		response =  (EightBallResponse)getWebServiceTemplate().marshalSendAndReceive(request);
		responseString = response.getAnswer().toString();
		return responseString;
	}
}
</pre></p>
<h2>Testing</h2>
<p>Now to test our Web Service Client.  We could have created our <strong>EightBallClient.java</strong> with a <strong>main</strong> and written code to exercise our web service client.  Instead we&#8217;ll write a <strong>Unit Test</strong> using Springs built-in support of JUnit.</p>
<h3>JUnit Test</h3>
<ol>
<li>In the resource folder <strong>src/test/java</strong> in the package <strong>client8ball.client8ball</strong> create a unit test by selecting the package and then pressing the <strong>right mouse button</strong> and selecting <strong>New/JUnit Test Case</strong></li>
<li>Fill in the wizard specifying:
<ol>
<li>The <strong>Name</strong> of <strong>TestClient8Ball</strong></li>
<li>Selecting <strong>constructor</strong></li>
<li>Specifying the <strong>Class under test:</strong> as <strong>client8ball.client8ball.EightBallClient</strong></li>
<p><img src="http://jamesbnuzzo.files.wordpress.com/2009/08/junittestcase_2.gif?w=450" alt="JUnit Test Case for Client8Ball" /></p>
<li>After selecting the <strong>Next&gt;&gt;</strong> button :
<ol>
<li>Select the method <strong>AskQuestion</strong> and then select the <strong>Finish</strong> button</li>
</ol>
</li>
</ol>
<li>To implement our <strong>testAskQuestion</strong> method we will:
<ol>
<li>Load up the <strong>application context</strong> from the <strong>application-context.xml</strong> and <strong>EightBallClient.class</strong></li>
<li>We create an instance of the <strong>EightBallClient</strong></li>
<li>Now we invoke the client and <strong>Ask our Question</strong></li>
<li>Lastly we assess the response to our question</li>
</ol>
</li>
</ol>
<p><pre class="brush: java;">
package client8ball.client8ball;

import java.io.IOException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import junit.framework.TestCase;

public class TestClient8Ball extends TestCase {

public TestClient8Ball(String name) {
super(name);
}

public void testAskQuestion() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext (&quot;../../applicationContext.xml&quot;, EightBallClient.class);
EightBallClient client8Ball = (EightBallClient) applicationContext.getBean(&quot;eightBallClient&quot;);
try {
String answer = client8Ball.AskQuestion(&quot;Will This Work&quot;);
assertNotNull(answer);
System.out.println(&quot;The Answer is: &quot; + answer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}

}
</pre><br />
<strong> BE SURE YOUR Magic 8-Ball Web Service is up and running in you container (i.e., http://localhost:8080/eightball)</strong></p>
<ul>
<li>Test and have fun.</li>
</ul>
<h2> Project Code </h2>
<p>You can download <a href="http://www.4shared.com/file/134688113/1b131d5c/client8ball.html">Client8Ball Source</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jamesbnuzzo.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jamesbnuzzo.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jamesbnuzzo.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jamesbnuzzo.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jamesbnuzzo.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jamesbnuzzo.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jamesbnuzzo.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jamesbnuzzo.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jamesbnuzzo.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jamesbnuzzo.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jamesbnuzzo.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jamesbnuzzo.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jamesbnuzzo.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jamesbnuzzo.wordpress.com/59/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesbnuzzo.wordpress.com&amp;blog=6412760&amp;post=59&amp;subd=jamesbnuzzo&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesbnuzzo.wordpress.com/2009/09/22/spring-web-service-client-eightballdemo/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/71d5fa4384ecade10c6278c30e079094?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cyclocrossboy</media:title>
		</media:content>

		<media:content url="http://jamesbnuzzo.files.wordpress.com/2009/06/dependency.gif" medium="image">
			<media:title type="html">dependency</media:title>
		</media:content>

		<media:content url="http://jamesbnuzzo.files.wordpress.com/2009/04/jaxbeightball.gif" medium="image">
			<media:title type="html">JAXB Artifacts</media:title>
		</media:content>

		<media:content url="http://jamesbnuzzo.files.wordpress.com/2009/08/junittestcase_2.gif" medium="image">
			<media:title type="html">JUnit Test Case for Client8Ball</media:title>
		</media:content>
	</item>
		<item>
		<title>Spring Web Service&#8217;s with JAXB, Maven &amp; Eclipse</title>
		<link>http://jamesbnuzzo.wordpress.com/2009/04/14/spring-web-services-with-jaxb-maven-amp-eclipse/</link>
		<comments>http://jamesbnuzzo.wordpress.com/2009/04/14/spring-web-services-with-jaxb-maven-amp-eclipse/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 12:17:05 +0000</pubDate>
		<dc:creator>cyclocrossboy</dc:creator>
				<category><![CDATA[Spring WS]]></category>
		<category><![CDATA[Springframework]]></category>
		<category><![CDATA[Contract-First]]></category>
		<category><![CDATA[JAXB 2]]></category>
		<category><![CDATA[Maven 2]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring WS Example]]></category>

		<guid isPermaLink="false">http://jamesbnuzzo.wordpress.com/?p=5</guid>
		<description><![CDATA[Acknowledgements Technorati Profile Before I get started I wish to acknowledge the work of others that greatly assisted me in my understanding of Spring Web Services. I encourage readers to reach out to these sources as well. Blogs Tejakantamneni’s Weblog Ice09 David Winterfeldt Brian Dussault a colleague, a mentor and a very bright guy Contract [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesbnuzzo.wordpress.com&amp;blog=6412760&amp;post=5&amp;subd=jamesbnuzzo&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2> Acknowledgements</h2>
<p><a href="http://technorati.com/claim/bbb4yz42mq" rel="me">Technorati Profile</a></p>
<p>Before I get started I wish to acknowledge the work of others that greatly assisted me in my understanding of Spring Web Services.  I encourage readers to reach out to these sources as well.</p>
<ul>
<li>Blogs</li>
<ul>
<li><a href="http://tejakantamneni.wordpress.com/">Tejakantamneni’s Weblog</a></li>
<li><a href="http://ice09.wordpress.com/category/spring-web-services/">Ice09</a></li>
<li><a href="http://www.springbyexample.org/examples/simple-spring-web-services.html">David Winterfeldt</a></li>
</ul>
<li><a href="http://www.linkedin.com/in/briandussault">Brian Dussault</a> a colleague, a mentor and a very bright guy</li>
</ul>
<h2>Contract Last vs Contract First</h2>
<p>There are two development styles for developing web services using Java. With contract-last you start with your Java code and from that you generate the web service contract (WSDL &#8211; web service definition language). When using the contract first you start with the WSDL or contract and use Java to implement a service to meet this contract.</p>
<p>Spring-WS only supports contract-first development style.<br />
For more information on <a href="http://static.springsource.org/spring-ws/sites/1.5/reference/html/why-contract-first.html">contract-first</a> ; much of the information paraphrased here is explored in greater detail.</p>
<h2>Problems with Contract Last</h2>
<p>From experience and for me the most important difficulties with Contract Last are:</p>
<ul>
<li>Language specific (proprietary) types can become exposed in a XSD and are not portable to other languages.
<ul>
<li>Example Java TreeMap type would not be understood correctly by .NET or Python and would result in a different data type instantiated with differing semantics.</li>
</ul>
</li>
<li>Code == Contract. Increased Fragility since the WSDL contract is directly generated from the Java code thereby resulting in a change in WSDL contract with any change in the Java code. This is <strong>not</strong> desirable.
<ul>
<li>Contracts should be as stable as long as possible</li>
</ul>
</li>
<li>Performance resulting from XML generated from Java code.</li>
<li>XSD reuse is difficult if not impossible.</li>
</ul>
<h2>Benefits of Contract-First</h2>
<ul>
<li>By focusing on the contract and in particular the XSD that defines the contract you implement a more robust WSDL contract that can support changes in the Java code that implements the contract. That is the WSDL contract and Java contract are loosely coupled.
<ul>
<li>The contract is stable over a longer period of time</li>
</ul>
</li>
<li>By focusing on what is needed in the XML to support the WSDL contract a much better performing XML message is implemented.</li>
<li>XSD components are reusable across other WSDL contracts therefore increasing the re-usability of the SOA artifacts.
<ul>
<li>This of course is dependent upon the XML design pattern chosen to implement the XSD.  For an extensive and well thought exploration of this topic refer to Roger L. Costello&#8217;s discussion of <a href="http://www.xfront.com/BestPracticesHomepage.html">Schemas</a></li>
</ul>
</li>
</ul>
<h2>Tools Used</h2>
<p>For our example a number of tools are used.  Briefly I mention them here.</p>
<ul>
<li><a href="http://www.eclipse.org/platform">Eclipse IDE 3.4.1</a></li>
<li><a href="http://tomcat.apache.org/download-55.cgi">Tomcat 5.5</a></li>
<li><a href="http://maven.apache.org/">Maven 2</a>
<ul>
<li>For information and directions on installing the <a href="http://m2eclipse.codehaus.org/">Maven 2 plug-in in Eclipse.</a></li>
<li>For directions on how to set up the dependencies for Log4J logging refer to the following two sites, they provide the necessary info to get you set up:
<ol>
<li><a href="http://onemanwenttomow.wordpress.com/2007/12/31/maven2-log4j-and-jmx-dependencies/">One Man went to Mow</a></li>
<li> <a href="http://dotal.wordpress.com/2008/05/06/log4j-1215-and-maven-dependencies/">DoTaL</a></li>
<li> <a href="http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html">How to Register Jars</a></li>
</ol>
</li>
</ul>
</li>
<li><a href="https://jaxb.dev.java.net/">JAXB 2</a>. In our example we will use the standalone version.</li>
</ul>
<h2>Spring Web Services</h2>
<ul>
<li>The contract is defined by the XML schema (XSD) of the services&#8217; messages</li>
<li> The Service contract (i.e., WSDL) will be generated from the XSD</li>
<li> Each Spring-WS has an end point which handles the incoming XML documents and manages the outgoing XML response documents</li>
<li> Each end point will require a business service to operate
<ul>
<li> This business service is defined as an interface</li>
<li> The business service interface_ is implemented by a business service class</li>
</ul>
</li>
<li> These dependencies are injected by Springs IoC with metadata defined in the spring-ws-servlet.xml
<ul>
<li> All the components are <em>wired</em> together by the configurations specified in spring-ws-servlet.xml context.</li>
</ul>
</li>
</ul>
<h2>Sample: Magic 8 Ball</h2>
<p>The following example is based on the old toy <a href="http://en.wikipedia.org/wiki/Magic_8-Ball">Magic 8-ball</a> which allows as question to be asked and magically 1 of 20 possible answers return as a response to the query.   Our sample web service using Spring WS with JAXB will do just this.</p>
<h3>The Contract</h3>
<p>We focus on the <em>data</em> contract between our client and service and define this with in an XML Schema; our XSD.   Using a simple Russian Doll design pattern we have two elements; a request and a response.  The request involves a <em>question</em> and the reponse an <em>answer</em>.</p>
<p><pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;xs:schema targetNamespace=&quot;http://acompany.com/it/enterprise/DEPT/EightBall/v1&quot; xmlns:tns=&quot;http://acompany.com/it/enterprise/DEPT/EightBall/v1&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; elementFormDefault=&quot;unqualified&quot; attributeFormDefault=&quot;unqualified&quot;&gt;
	&lt;xs:element name=&quot;eightBallRequest&quot;&gt;
		&lt;xs:complexType&gt;
			&lt;xs:sequence&gt;
				&lt;xs:element name=&quot;Question&quot; type=&quot;xs:string&quot;/&gt;
			&lt;/xs:sequence&gt;
		&lt;/xs:complexType&gt;
	&lt;/xs:element&gt;
	&lt;xs:element name=&quot;eightBallResponse&quot;&gt;
		&lt;xs:complexType&gt;
			&lt;xs:sequence&gt;
				&lt;xs:element name=&quot;Answer&quot; type=&quot;xs:string&quot;/&gt;
			&lt;/xs:sequence&gt;
		&lt;/xs:complexType&gt;
	&lt;/xs:element&gt;
&lt;/xs:schema&gt;
</pre></p>
<h3>Marshalling/Unmarshalling:JAXB</h3>
<p>Not only does our XSD define our data contract between the client and service it is used by JAXB to generate the marshalling and unmarshalling code that our service uses to handle the XML messages.</p>
<ol>
<li>With your XSD in the <strong>bin</strong> directory of you JAXB installation run the <strong>xjc</strong> tool to generate the JAVA class for marshalling and unmarshalling.</li>
<li>You can us the <strong>-p</strong> switch and specify a simple package name for your JAXB JAVA code. In our example <strong>jaxb.eightball</strong>.  The default package name is the XSD <em>target namespace</em></li>
</ol>
<blockquote><p>C:\jaxb-ri-20070122\bin&gt;xjc.bat -p jaxb.eightball eightball.xsd<br />
parsing a schema&#8230;<br />
compiling a schema&#8230;<br />
jaxb\eightball\EightBallRequest.java<br />
jaxb\eightball\EightBallResponse.java<br />
jaxb\eightball\ObjectFactory.java<br />
jaxb\eightball\package-info.java</p></blockquote>
<h3>Eclipse Project using Maven 2</h3>
<ol>
<li>In Eclispe use <em>File/New/Project&#8230;/Maven Project</em></li>
<li>Select <strong>Default workspace</strong></li>
<li>Select <strong>spring-ws-archtype</strong></li>
<li>Enter <strong>eightballdemo</strong> for <em>Group Id</em></li>
<li>Enter <strong>eightballdemo</strong> for <em>Artifact Id</em></li>
<li>Select <strong>Finish</strong></li>
<li>Selecting the <strong>POM.XML</strong> under the <strong>dependencies</strong> tab add the log4j POM dependencies in the <strong><em>Tools Used</em></strong> section above
<ul>
<li>Add <strong>spring-oxm-tiger</strong> dependency</li>
<li>Add <strong>spring-ws-core-tiger</strong> dependency</li>
</ul>
</li>
<p><img src="http://jamesbnuzzo.files.wordpress.com/2009/04/pom_depend.gif?w=450" alt="POM Dependencies" /></ol>
<h3>Exposing the Contract</h3>
<ul>
<li>Import <strong>eightball.xsd</strong> in <strong><em>src/main/webapp/WEB-INF</em></strong></li>
<li>Edit <em>spring-ws-servlet.xml</em> and add the following for Spring to generate the WSDL at runtime from the XSD file:</li>
<p><pre class="brush: xml;">
   &lt;!-- Add automatic WSDL generation support --&gt;
    &lt;bean id=&quot;eightball&quot; class=&quot;org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition&quot;&gt;
&lt;property name=&quot;schema&quot; ref=&quot;schema&quot; /&gt;
&lt;property name=&quot;portTypeName&quot; value=&quot;eightball&quot; /&gt;
&lt;property name=&quot;locationUri&quot; value=&quot;http://localhost:8080/eightball/&quot; /&gt;
&lt;property name=&quot;targetNamespace&quot; value=&quot;http://acompany.com/it/enterprise/DEPT/EightBall/v1&quot; /&gt;
    &lt;/bean&gt;  

    &lt;bean id=&quot;schema&quot; class=&quot;org.springframework.xml.xsd.SimpleXsdSchema&quot;&gt;
&lt;property name=&quot;xsd&quot; value=&quot;/WEB-INF/eightball.xsd&quot; /&gt;
    &lt;/bean&gt;
</pre></ul>
<h3>Preparing for Code</h3>
<ol>
<li>Add a <strong>Source Folder</strong> to your project: <strong><em>src/main/java</em></strong></li>
<li>Add the following <strong>packages</strong> to this <strong>Source Folder</strong></li>
<ul>
<li><strong>ws</strong> &#8211; web service <em>end point</em></li>
<li><strong>jaxb.eightball</strong> &#8211; <em>JAXB</em> marshalling and unmarshalling classes</li>
<li><strong>service</strong> &#8211; interface for class that contains service logic</li>
<li><strong>service.impl</strong> &#8211; class that implements service logic</li>
</ul>
<li><strong>Import</strong> the JAXB Java files generated under the <strong>jaxb.eightball</strong> package.</li>
<p><img src="http://jamesbnuzzo.files.wordpress.com/2009/04/jaxbeightball.gif?w=450" alt="JAXB for Eightball" /></p>
</ol>
<h3>EndPoint</h3>
<h4>First Part</h4>
<ul>
<li>In <strong><em>spring-ws-servlet.xml</em></strong> define the web service endpoint using annotations along with the service interface and implementation.</li>
<p><pre class="brush: java;">
&lt;!-- Register PayloadRootAnnotationMethodEndpointMapping --&gt;  
    &lt;bean class=&quot;org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping&quot; /&gt;  
  
    &lt;!-- Register Endpoint --&gt;  
    &lt;bean id=&quot;eightBallEndpoint&quot; class=&quot;ws.EightBallEndpoint&quot;&gt;
    &lt;description&gt;
            This endpoint handles requests.
        &lt;/description&gt;
        &lt;property name=&quot;eightBallService&quot; ref=&quot;eightBallService&quot;/&gt;
    &lt;/bean&gt;  
    
     &lt;bean id=&quot;eightBallService&quot; class=&quot;service.impl.EightBallServiceImpl&quot;&gt;
        &lt;description&gt;
            This bean is our &quot;business&quot; service.
        &lt;/description&gt;
    &lt;/bean&gt;
    
</pre></p>
<li>Now create the end point class <strong>EightBallEndPoint</strong> in the <strong>ws</strong> package</li>
<p><pre class="brush: java;">
package ws;
import service.EightBallService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;

import jaxb.eightball.EightBallRequest;
import jaxb.eightball.EightBallResponse;

@Endpoint
public class EightBallEndpoint {
	
	private EightBallService eightBallService;
	protected final Log logger = LogFactory.getLog(getClass());
	
	/**
	 * Sets the &quot;business service&quot; to delegate to.
	 */
	public void setEightBallService(EightBallService eightBallService) {
		this.eightBallService = eightBallService;
	}....
</pre>
</ul>
<h3>Service: EightBallService</h3>
<ul>
<li>Define the interface <strong>EightBallService</strong> in the package <strong>service</strong></li>
<p><pre class="brush: java;">
package service;

public interface EightBallService {

	String question (String s);
}
</pre></p>
</ul>
<h3>Wire In the JAXB</h3>
<ul>
<li>In <strong><em>spring-ws-servlet.xml</em></strong> add the following wiring for the JAXB marshalling and unmarshalling.</li>
<p><pre class="brush: xml;">
     &lt;!-- Configure XML Marshaller --&gt;  
    &lt;bean class=&quot;org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter&quot;&gt;  
        &lt;constructor-arg ref=&quot;marshaller&quot; /&gt;  
    &lt;/bean&gt;  
  
    &lt;bean id=&quot;marshaller&quot; class=&quot;org.springframework.oxm.jaxb.Jaxb2Marshaller&quot;&gt;  
        &lt;property name=&quot;classesToBeBound&quot;&gt;  
            &lt;list&gt;  
                &lt;value&gt;jaxb.eightball.EightBallRequest&lt;/value&gt;  
                &lt;value&gt;jaxb.eightball.EightBallResponse&lt;/value&gt; 
            &lt;/list&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt; 
</pre></p>
<li>In <strong>EightBallEndPoint</strong> add the following annotation based section for marshalling and unmarshalling.</li>
<p><pre class="brush: java;">

	@PayloadRoot(localPart= &quot;eightBallRequest&quot;, namespace=&quot;http://acompany.com/it/enterprise/DEPT/EightBall/v1&quot;)
	public EightBallResponse doit(EightBallRequest request){

		String question = request.getQuestion();
		System.out.println(&quot;The question is &quot; + question);
		logger.info(&quot;The question is &quot; + question);
		String answer = eightBallService.question(question);
		logger.info(&quot;The answer is &quot; + answer);
		EightBallResponse response = new EightBallResponse();
		response.setAnswer(answer);
		return response;
	}
}
</pre>
</ul>
<h3>EightBallServiceImpl</h3>
<ul>
<li>Create the Class <strong>EightBallServiceImpl</strong> implementing the interface <strong>EightBallService</strong></li>
<p><pre class="brush: java;">
package service.impl;

import java.util.ArrayList;
import java.util.Random;

import service.EightBallService;

public class EightBallServiceImpl implements EightBallService {

	public String question (String s) {
		Random rand = new Random();
		ArrayList&lt;String&gt; answers = new ArrayList&lt;String&gt;(20);
		answers.add(0, &quot;As I see it, yes&quot;);
		answers.add(1, &quot;It is certain&quot;);
		answers.add(2,&quot;It is decidedly so&quot; );
		answers.add(3,&quot;Most likely&quot;);
		answers.add(4,&quot;Outlook good&quot;);
		answers.add(5, &quot;Signs point to yes&quot;);
		answers.add(6, &quot;Without a doubt&quot;);
		answers.add(7, &quot;Yes&quot;);
		answers.add(8, &quot;Yes - definitely&quot;);
		answers.add(9, &quot;You may rely on it&quot;);
		answers.add(10,&quot;Reply hazy, try again&quot;);
		answers.add(11,&quot;Better not tell you now&quot;);
		answers.add(12,&quot;Cannot predict now&quot;);
		answers.add(13,&quot;Concentrate and ask again&quot;);
		answers.add(14,&quot;Don't count on it&quot;);
		answers.add(15,&quot;My reply is no&quot;);
		answers.add(16,&quot;My sources say no&quot;);
		answers.add(17,&quot;Outlook not so good&quot;);
		answers.add(18,&quot;Very doubtful&quot;);
		answers.add(19, &quot;Ask again later&quot;);

		int iNum = rand.nextInt(20);
		return answers.get(iNum).toString();
	}
}


</pre></p>
</ul>
<h3>Package and Test</h3>
<p>We are now done should be ready to compile, package and test our service.  I strongly recommended using <a href="http://www.soapui.org/">SoapUI </a>for testing your web service.</p>
<ul>
<li>On the project <strong>eightballdemo</strong> <em>right mouse-button</em></li>
<ul>
<li>Select <strong>Run As</strong></li>
<li>Select <strong>Maven package</strong></li>
</ul>
<li>You can deploy your <strong>eightball.war</strong> to your Tomcat servlet container</li>
<li>From SoapUI you can create a project and import the WSDL from your Tomcat Instance</li>
<p><code>http://localhost:8080/eightball/eightball.wsdl</code></p>
<li>Test and have fun</li>
</ul>
<h2>Project Code</h2>
<p>You can download <a href="http://www.4shared.com/file/122708510/d14549fa/eightballdemo.html">Eclipse Project Zip file</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jamesbnuzzo.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jamesbnuzzo.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jamesbnuzzo.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jamesbnuzzo.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jamesbnuzzo.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jamesbnuzzo.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jamesbnuzzo.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jamesbnuzzo.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jamesbnuzzo.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jamesbnuzzo.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jamesbnuzzo.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jamesbnuzzo.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jamesbnuzzo.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jamesbnuzzo.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesbnuzzo.wordpress.com&amp;blog=6412760&amp;post=5&amp;subd=jamesbnuzzo&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesbnuzzo.wordpress.com/2009/04/14/spring-web-services-with-jaxb-maven-amp-eclipse/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/71d5fa4384ecade10c6278c30e079094?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cyclocrossboy</media:title>
		</media:content>

		<media:content url="http://jamesbnuzzo.files.wordpress.com/2009/04/pom_depend.gif" medium="image">
			<media:title type="html">POM Dependencies</media:title>
		</media:content>

		<media:content url="http://jamesbnuzzo.files.wordpress.com/2009/04/jaxbeightball.gif" medium="image">
			<media:title type="html">JAXB for Eightball</media:title>
		</media:content>
	</item>
	</channel>
</rss>
