Java Apache Axis2 web services client examples advanced
Summarize
Summary of Java Apache Axis2 web services client examples advanced
This document provides advanced examples and guidance for constructing and using an Apache Axis2 client to consume ServiceNow SOAP web services. Axis2 is a Java-based SOAP engine framework used to build SOAP clients, servers, or gateways. The content is tailored for system administrators with basic Java development experience.
Show less
Prerequisites
- Java JDK version 1.4.2 or higher
- Apache Axis2 version 1.0 or higher
- Eclipse SDK (optional) for managing Java projects and executing web requests
Creating and Setting Up the Java Project
- Create a Java project in Eclipse or your preferred IDE, ensuring the correct JRE version matches your Axis2 client generation method.
- Use the
wsdl2javatool from Axis2 to generate client stub classes from the ServiceNow WSDL URL or a local WSDL file. - Refresh your project to see the generated stub and callback handler classes.
Using Basic Authentication
To authenticate with ServiceNow, use the HttpTransportProperties.Authenticator class to supply the username and password, then set this authenticator on the Axis2 client options:
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);
Handling HTTP Chunking
ServiceNow does not support HTTP chunking, which is enabled by default in Axis2 versions 1.1 and higher. To ensure compatibility, chunking must be disabled either at deployment or runtime:
- Deployment time: Remove or comment out the
<parameter name="Transfer-Encoding">chunked</parameter>entry in theAxis2.xmlconfiguration file. - Runtime: Disable chunking programmatically by setting the following property on the client or stub options (Axis2 1.1.1+ only):
options.setProperty(org.apache.axis2.transport.http.HTTPConstants.CHUNKED, Boolean.FALSE);
Creating Unique Java Packages
To avoid naming conflicts, you can map XML namespaces in the WSDL to unique Java package names by using the namespace2package (-ns2p) parameter during client code generation. For example:
wsdl2java.bat -u -p cr2 -ns2p http://www.service-now.com/changerequest=my.changerequest -uri changerequest
This command generates client classes in the my.changerequest package for the specified namespace.
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.
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 1. 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