Web Service Clients with wsimport and JAX-WS

This page describes how to use wsimport to create, configure and use web service clients with Maven projects.


  • Generate web service client java files
  • Changing the URL where the service points to
  • Authentication (BASIC, Digest, NTLM)



Currently the issue discussed at the following URL prevents eclipse from automatically generating source code from a WSDL.

Until someone provides a connector to solve this issue, you can use the wsimport command provided with your JDK.

Generating the Service Client

If you already have a web service you want to connect to then create. Otherwise you can follow the instructions provided here to create one.

Using the command line navigate to the project’s base folder.

wsimport -extension -p com.pack.name -s src/main/java -Xnocompile src/wsdl/stockquote.wsdl

Once the java files are generated you can package it into a JAR and check it into nexus. From there you can use it as a dependency for any other project.

Pointing the service to a different URL

Sometime it is helpful to point a service to a URL for QA testing and a different URL for production. This can be accomplished without regenerating the service.


The three popular authentication methods to protect a web service are:

  1. BASIC
  2. Digest
  3. NTLM (Windows Services)

No matter what method the server uses to authenticate you need to create your own custom Authenticator.

class MyAuthenticator extends Authenticator {
	private final String user;

	/** There are three options for specifying the domain in this field.
	<p>1. Do not specify it. In some environments, the domain is not actually required 
    and the application need not specify it. 
    <p>2. The domain name can be encoded 
    within the username by prefixing the domain name followed by a back-slash 
    '\' before the username. With this method, existing applications that use 
    the Authenticator class do not need to be modified, so long as users are 
    made aware that this notation must be used.
    <p>3. If a domain name is not specified as in method 2) and the system property 
    "http.auth.ntlm.domain" is defined, then the value of this property will 
    be used as the domain name.
    private final String password;
    public MyAuthenticator(String user, String password) {
               this.user = user;
               this.password = password;
    public PasswordAuthentication getPasswordAuthentication () {           
    /* During this call any of the follwing may be called 
    	to decide what to return
	    return new PasswordAuthentication (user, password.toCharArray());        

To use this in your code do the following:

For NTLM see the javadoc instructions for the username field. For other types just enter username and password as usual.

MyAuthenticator auth = new MyAuthenticator("testdomain\\wstest", "luckyme");

The following is the JAX-WS method: (you may want to try this instead?)

    YourClassName port = service.getPort(YourClassName.class);
    BindingProvider bp = (BindingProvider) port;
    bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "username");
    bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "password");

Basic Authentication

Setup Tomcat

Although the below is the default configuration please, verify that the following files looks like this:


<?xml version='1.0' encoding='utf-8'?>
  <role rolename="admin"/>
  <role rolename="tomcat"/>
  <role rolename="manager"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user name="admin" password="admin" roles="admin,manager" />

For the above file you may need to un-comment the above lines.

Also verify that the following sections are present inside:


    <Resource name="UserDatabase" auth="Container"
              description="User database that can be updated and saved"
              pathname="conf/tomcat-users.xml" />
  <Realm className="org.apache.catalina.realm.UserDatabaseRealm"

Accessing the service

There are 2 methods to get a reference to the web service.

Using Service.create()

The following could be used to change the URL to another server:

URL url = new URL("http://localhost:9999/ws/hello?wsdl");
QName qname = new QName("http://server/", "HelloWorldImplService");
Service service = Service.create(url, qname);
HelloWorld helloWorld = service.getPort(HelloWorld.class);

Using the accessor methods on the Generated Sources

WSimport generates client class that has a built in getters to obtain a reference to the service. I don’t prefer this method since it defaults to the URL specified in the WSDL file. There may be other ways to specify the URL however the method above suits me well.


3 Responses to “Web Service Clients with wsimport and JAX-WS”

  1. December 10, 2015 at 7:14 am

    Thank you very very very much! I spend six hours of searching solution for digest secured ws. Method Authentication.setDefault(auth) is working for me.

Leave a Reply

Please log in using one of these methods to post your comment:

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

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 77 other followers

January 2013
« Nov   Feb »

Blog Stats

  • 847,163 hits

%d bloggers like this: