JAX-WS clients and embedded WSDL files

Search stackoverflow.com for JAX-WS and WSDL and you will see a lot of people asking questions like this:

The problem is i need to build a web service client from a file i’m been provided. I’ve stored this file on the local file system and , while i keep the wsdl file in the correct file system folder, everithing is fine. When i deploy it to a server or remove the wsdl from the file system folder the proxy can’t find the wsdl and rises an error.

The common suggestion to use the “wsdlLocation” command line option or the equivalent maven plugin configuration directive does not work. This suggestion does work though.

Here is a portable way to create a web service client. Say the WSDL file is in the directory “src/main/resources/wsdl/CardValidator.wsdl” (assuming a maven project) inside the project, then the following code can be used to create the web service client and this code will work from inside a war, a jar or even from the exploded classes directory as well:

public class App {

    public void someMethod() {
        URL wsdlURL = App.class.getClassLoader().getResource("wsdl/CardValidator.wsdl");
        QName qname =
            new QName("http://ws.creditcard.jaxws.demo.nihilanth.net/", "CardValidatorService");
        CardValidatorService cardValidatorService =
            new CardValidatorService(wsdlURL, qname);
        CardValidator cardValidator = cardValidatorService.getCardValidatorPort();
Post a comment or leave a trackback: Trackback URL.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: