Enterprise Architect in HCL Technologies a $7Billion IT services organization. My role is to work as a Technology Partner for large enterprise customers providing them low cost opensource solutions around Java, Spring and vFabric stack. I am also working on various projects involving, Cloud base solution, Mobile application and Business Analytics around Spring and vFabric space. Over 23 yrs, I have build repository of technologies and tools I liked and used extensively in my day to day work. In this blog, I am putting all these best practices and tools so that it will help the people who visit my website. Krishna is a DZone MVB and is not an employee of DZone and has posted 64 posts at DZone. You can read more from them at their website. View Full User Profile

Calling a Webservice from Java using Maven

02.26.2013
| 4275 views |
  • submit to reddit

For people in hurry get the latest code @ github and run “mvn test”

Introduction

Typically, when you call a Webservice from Java, you need to create a Stub class and use that stub class to call the Webservice in Java. The Stub class will do the Marshalling of the data and send that data to the server. As a best practices,

  • The stub class should be generated by a tool
  • We need to build a JUnit test to make sure you test the generated stub for its method validity, for example if the method signature is changed
  • The Stub class is always generated and is never checked into version control so that your code quality tool will not measure the generated code

Details: Webservices with Maven

In this blog I will show you how to call a Webservice from Java, by having a JAXWS plugin in Maven and generating the stubs during build time. In this blog I am calling a standard Currency Conversion Webservice (http://www.webservicex.net/CurrencyConvertor.asmx?WSDL), where in you pass a source currency and target currency and it will return the exchange rate in real time. The Maven config is as below,

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>1.9</version>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
<configuration>
<wsdlUrls>
<wsdlUrl>
http://www.webservicex.net/CurrencyConvertor.asmx?WSDL
</wsdlUrl>
</wsdlUrls>
</configuration>
</plugin>
</plugins>
</build>

When you put the above jaxws plugin and run any Maven command like test, package, it generates the stubs in target/jaxws/wsimport/java folder. The Jaxws plugin is configurable to create stubs in any folder.

If you see the JUnit test it looks as below,

@Test
public void test() {
CurrencyConvertor currencyConvertor = new CurrencyConvertor();

assertTrue(currencyConvertor.getCurrencyConvertorSoap().conversionRate(Currency.INR, Currency.USD) >  0.0D);
}

If you want to test this in STSIDE, maven import the project and add build path for the source to target/jaxws/wsimport/java. Once you have it, you can Run as -> JUnit test, and the test will be successful.



 

Published at DZone with permission of Krishna Prasad, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)