Calling a Webservice from Java using Maven

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.

I hope you like it.

About these ads

5 thoughts on “Calling a Webservice from Java using Maven

  1. Frisian

    I’m not really sure, what you want to achieve with your unit test. If it’s about the method signature, the compiler should take care of that. It might be, that the service’s signature is so generic, that you want to check whether certain argument combinations are still valid. That would be a very bad design in the first place, but then again you might not be able to change it.
    As soon as it’s about verifying the correctness of the service operations themselves, one is testing someone else’s code and subsequently is doing some else’s work.

    Reply
    1. GoSmarter Post author

      Frisian, thanks for reading my blog. You are right, it is not a must that we write junit test always. But with Webservices that are developed by 3rd party, this extra step will carry a long way. In few of our projects, the 3rd party Webservices were not designed properly, the signature were generic, as you mentioned, so we need to check the arguments are still correct. For us this was just a safety “net”.

      And in some cases it makes sense to write JUnit, for example, all our projects are spring integration projects, where the web services are used just within SI flow. In this scenario, writing junit test case will atleast compile the code and make sure the signature is still valid.

      Reply
  2. work from home for free

    Hello there! Do you know if they make any plugins to help with Search Engine Optimization?
    I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good results.
    If you know of any please share. Thanks!

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s