Java Apache Axis2 web services client examples advanced
Summarize
Summary of Java Apache Axis2 Web Services Client Examples Advanced
This guide provides practical examples for ServiceNow customers on how to construct and use a Java Apache Axis2 client to consume ServiceNow SOAP web services. Apache Axis2 is a Java-based SOAP engine framework used to build clients, servers, or gateways for SOAP communication. The content targets system administrators with some Java development experience and requires Java JDK 1.4.2 or higher and Axis2 version 1.0 or higher.
Show less
Creating and Managing the Java Project
- You can create a Java project using Eclipse SDK (version 3.4.2 used in example), though Eclipse is not mandatory.
- Ensure the project uses the correct Java Runtime Environment (JRE) version that matches your Java installation.
Generating Axis2 Client Code
- Use the
wsdl2javatool to generate client stub code from the ServiceNow WSDL URL or a locally saved WSDL file. - Specify the output directory with the
-oparameter to control where generated files are saved. - After generation, refresh the project in your IDE to view the generated Stub and CallbackHandler classes, which facilitate web service calls.
Configuring Basic Authentication
- Set up HTTP basic authentication by creating an
Authenticatorinstance with your ServiceNow username and password. - Assign the authenticator to the service client’s HTTP transport properties to enable authenticated access to ServiceNow SOAP endpoints.
Handling Axis2 Version Compatibility and Chunking
- Axis2 versions 1.1 and higher enable HTTP chunking by default, but ServiceNow SOAP endpoints do not support chunking.
- Disable HTTP chunking either by:
- Removing or commenting out the
<parameter name="Transfer-Encoding">chunked</parameter>in the Axis2.xml configuration file (deployment time). - Setting the runtime property
HTTPConstants.CHUNKEDtofalseon the client or stub for Axis2 versions 1.1.1 and higher.
Creating Unique Java Packages for Generated Code
- Use the Axis2
namespace2package(ns2p) parameter during code generation to map XML namespaces to unique Java package names, avoiding naming conflicts. - This is specified with the format
-ns2p <namespace>=<package name>in thewsdl2javacommand.
Practical Benefits
Following these examples enables ServiceNow customers to efficiently generate and configure Java clients that reliably consume ServiceNow SOAP web services, handle authentication securely, and ensure compatibility with Axis2 versions while avoiding common pitfalls like unsupported HTTP chunking. This facilitates seamless integration of ServiceNow data and operations within Java-based applications.
Examples showing how to construct and use an Axis2 client to consume a ServiceNow Web Service.
Axis is essentially a SOAP engine -- a framework for constructing SOAP processors such as clients, servers, or gateways. The current version of Axis is written in Java. This content is intended for system admins with a light development background in Java. To begin you would need Java JDK version 1.4.2 or higher and Axis2 version 1.0 or higher.
Create a Java Project
- Open Eclipse and from the menu select .
- Give the project a name.
- Verify that the correct JRE is specified.
- If using wsdl2java run "java -version" on the command line and this will be the version to specify for the project specific JRE.
- If using the Axis2 Codegen plugin use default JRE.
Figure 1. Java Project
Generate your Axis2 client code
- From a command line in the bin directory of the axis
folder:
./wsdl2java.sh -uri https://<instance name>.service-now.com/incident.do?WSDL -o /glide/workspace/TestWebService/ - In the above example:
- The "-uri" is either the path where you have saved a copy of the wsdl to either ".wsdl" or ".xml", or the URL the WSDL resides at.
- The "-o" is the path where you want the files to be written out to. If not specified, the files will be written out to the current bin location.
- In Eclipse refresh the project and the generated Stub and CallbackHandler should now be
displayed
Figure 2. Axis Stub
Basic Authentication
HttpTransportProperties.Authenticator basicAuthentication = new HttpTransportProperties. Authenticator ( ) ;
basicAuthentication. setUsername ( "admin" ) ;
basicAuthentication. setPassword ( "admin" ) ;
...
ServiceNowStub proxy = new ServiceNowStub ( ) ;
...
proxy._getServiceClient ( ). getOptions ( ). setProperty (org. apache. axis2. transport. http. HTTPConstants. AUTHENTICATE, basicAuthentication ) ;
Compatibility with Axis2 Versions 1.1 and higher
- Deployment time: One can disable HTTP chunking by removing or commenting out the following
element from
Axis2.xml
<parameter name= "Transfer-Encoding" >chunked</parameter> - Runtime: User can disable the chunking using following property set in Client or Stub,
versions 1.1.1 and higher
only
options.setProperty (org. apache. axis2. transport. http. HTTPConstants. CHUNKED, Boolean. FALSE ) ;
Creating Unique Packages
<Axis path>\bin\wsdl2java.bat -u -p cr2 -ns2p <namespace>=<package name> -uri <wsdl to convert><Axis path>\bin\wsdl2java.bat -u -p cr2 -ns2p http://www.service-now.com/change_request=my.change_request -uri change_request