Java web client using JAX WS
Table of Contents
Java web client using JAX WS
System Requirements:
Client Code Auto Generation using JAX-WS
Implement Client and Header Handlers
Service Initialization
Handler Resolution
Object Creation
Finally, call the web service through SEI
Code Files (For VehiclePriceCalculation service only)
System Requirements:
JDK 1.6: Download from
Java IDE (optional): for example IDEA, Eclipse, NetBeans
Client Code Auto Generation using JAX-WS
Java API for XML-Based Web Services (JAX-WS) tooling supports generating Java artifacts needed to develop static JAX-WS Web services clients when starting with a Web Services Description Language (WSDL) file.
The static client programming model for JAX-WS is called the dynamic proxy client. The dynamic proxy client invokes a Web service based on a service endpoint interface that is provided. After a proxy is created, the client application can invoke methods on the proxy just like a standard implementation of those interfaces. For JAX-WS Web service clients using the dynamic proxy programming model, use the JAX-WS tool, wsimport, to process a WSDL file and generate portable Java artifacts that are used to create a Web service client. Create the following portable Java artifacts using the wsimport tool:
- Service endpoint interface (SEI)
- Service class
- Java Architecture for XML Binding (JAXB) generated type values which are Java classes mapped from XML schema types
One of the most important benefits of using JAX WS is that it is part of the Java Specifications and does not require any third party jars to be loaded in the classpath
Simple steps to follow
- Create directory where auto code needs to be generated, let’s say D:\Projects\client\java\src or /home/<user>/projects/ client/java/
- Create customBinding.xml with following content(attached in theCode Files section below)
- Open command prompt
- Go to directory created above
cd D:\Projects\client\java\src
- Run following command to start code generation process:
wsimport -s . –b <path to the above created xml file> <wsdl file path>
For e.g. D:\Projects\client\java\src>wsimport -s D:\Projects\custom\src -b customBinding.xml
For more information run wsimport –help
The above generated code can be compiled and added to a Java archive(jar) file and ported to any application as a single unit.
Implement Client and Header Handlers
A Client would be the point of interaction for the application trying to integrate with the webservice.
Client class would interact with Service Endpoint Interface created above to make a call to webservice.
Following explanation would present a sample java web client created for VehiclePriceCalculation
Service Initialization
First part of Client would be initialization of service endpoint.
Sample Code Snippet:
Handler Resolution
The JAX-WS programming model provides an application handler facility that enables us to manipulate a message on either an inbound or an outbound flow. This Handler sniffs the outgoing SOAP Requests and adds the required security credentials. If the security policy or framework changes, all we need to do is create a new handler and associate it with the client, so it won't require any code changes at the client end.
Sample Code Snippet:
OasisHeaderHandler.java is attached in the next section.
Object Creation
The wsimport has generated type values which are Java classes mapped from XML schema types. These types are essentially the input and output object types of the service endpoint interface.
Sample Code Snippet:
Finally, call the web service through SEI
Sample Code Snippet:
Code Files (For VehiclePriceCalculation service only)
Custom Binding XML
Generated code:
Client Classes:
Handler Class:
Complete Package