Click here to Skip to main content
11,806,590 members (43,120 online)
Click here to Skip to main content

Calling a Webservice from Java using Maven

, 21 Dec 2012 CPOL 6.7K 2
Rate this:
Please Sign up or sign in to vote.
This tip shows how you can create stubs for webservices in Java using Maven


This tip shows how you can create stubs for webservices in Java using Maven.


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 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 (, wherein you pass a source currency and target currency and it will return the exchange rate in real time. The Maven config is as below:


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:

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

			(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.

Here is the location to get the latest code @ github and run "mvn test".

I hope this tip helped you.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
sesh192-Jan-13 2:59
membersesh192-Jan-13 2:59 
GeneralMy vote of 5 Pin
Aravind Kakarla31-Dec-12 3:57
memberAravind Kakarla31-Dec-12 3:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151002.1 | Last Updated 22 Dec 2012
Article Copyright 2012 by skprasadu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid