Create a Client with JAX-WS

We're going to move another level up the Java web services technology stack. Up to this point, we have:

Now we can graduate to a Java client that creates messages based on classes generated from a local, modified version of an AppFxWebService WSDL and...

Uses web references to the actual endpoint.

This will greatly simplify your code!

Create the Client Project

Create a Java application in NetBeans: AppFxWebServiceClientJAXWS.

Right-click the project and select NewWeb Service Client. You may have to navigate to Other and choose the File Type through the New File screen.

Or...

The New Web Service Client screen appears.

Specify the WSDL file location as the location of the modified WSDL file described in Create Modified WSDL for AppFxWebService to Bind Using JAXB or JAX-WS. Here is a copy: appfxwebservice.asmx.wsdl.

Also, select a package.

Click Finish. The wizard runs wsimport and adds the new Generated Sources (jax-ws) and Web Service References.

Expand the Web Service References node.

Right-click the node and select Edit Web Service Attributes.

A configuration screen appears.

Select the Wsimport Options tab.

Change wsdlLocation to the hosted location for the WSDL. For example: http://localhost/bbAppFx/appfxwebservice.asmx?WSDL.

Refresh the reference. Right-click the reference and select Refresh.

A confirmation appears.

Don't select the option. Click Yes. The IDE runs wsimport again.

Create an Object for the Service

Add this code to your main method:

        AppFxWebService appFxWebService = new AppFxWebService();

Do Something with It

Add this code to your main method:

        PingRequest p = new PingRequest();
        p.setMessageToEcho("Hello there");
        
        ClientAppInfoHeader cai = new ClientAppInfoHeader();
        p.setClientAppInfo(cai);

This creates a ping request message, adds a message string to the request, creates a header for ClientAppInfo, and adds the header to the ping request message.

Having coded the message formulation, add this:

        System.out.println(appFxWebService.getAppFxWebServiceSoap12().ping(p).getEchoedMessage());

This sends the message and output the reply message.

Compilation and run output:

init:
Deleting: C:\Users\TomTr\Documents\NetBeansProjects\AppFxWebServiceClientJAXWS\build\built-jar.properties
deps-jar:
Updating property file: C:\Users\TomTr\Documents\NetBeansProjects\AppFxWebServiceClientJAXWS\build\built-jar.properties
wsimport-init:
wsimport-client-appfxwebservice.asmx:
files are up to date
wsimport-client-generate:
Compiling 1 source file to C:\Users\TomTr\Documents\NetBeansProjects\AppFxWebServiceClientJAXWS\build\classes
compile:
run:
Hello there
BUILD SUCCESSFUL (total time: 7 seconds)