How to add a custom SOAP header to the request using AXIOM

Suppose you want to add the following SOAP header block to your web service request message.
<myNS:header xmlns:myNS="">
This is a custom soap header
</myNS:header >

There are different approaches to add user defined headers to the request soap messages. Lets see how it could be done using AXIOM in simpler way.
In this example we are going to invoke Axi2 default version service with adding a custom soap header in to the request.

Download and install Apache Axis2
Install Apache Tcpmon

Step 1

Start Axis2 server by running AXIS2_HOME/bin/axis2server.bat{sh}
Go to http://localhost:8080. You will see that the default version service is deployed there.

Step 2

Now, we need to generate client stubs. Go to AXIS2_HOME/bin and run wsdl2java.bat{sh} with the following parameters.
WSDL2Java -uri http://localhost:8080/axis2/services/Version?wsdl -o out -uw

The client stubs will be generated in a directory called "out".

Now, write a client importing the generated stub classes as follows(You can easily create a project in Eclipse using the generated Build.xml)

import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;
import sample.axisversion.ExceptionException0;
import sample.axisversion.VersionStub;

public class CustomSoapHeaderClient {

public static void main(String[] args) throws AxisFault{

String url = "http://localhost:8090/axis2/services/Version";
VersionStub stub = new VersionStub(url);

OMFactory omFactory =OMAbstractFactory.getOMFactory();
OMNamespace omNamespace = omFactory.createOMNamespace("", "myNS");
OMElement header = omFactory.createOMElement("header", omNamespace);
header.setText("This is a custom soap header");

try {
} catch (RemoteException e) {
} catch (ExceptionException0 e) {


Note the highlighted code which creates the custom soap header.

Step 3

We can visualize the soap request using Tcpmon. Therefore open tcpmon and configure listen port in 8090 and target port 8080.
Compile and run the above client. You will see the following message in request pane of Tcpmon.

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="">
<myNS:header xmlns:myNS="">This is a custom soap header</myNS:header>


Mari Raja said…
Hi Charitha,

I used ur code. But I am getting the following error,

org.apache.axis2.AxisFault: Missing wsse:Security header in request
at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(
at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(
at org.apache.axis2.description.OutInAxisOperationClient.send(
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(
at org.apache.axis2.client.OperationClient.execute(

How can I fix it. Please help me.

Mari Raja.
Charitha said…
Hi Mari,
It seems you have engaged "apache rampart" module at your web service. If you did so, you must have the necessary security policies at the client side.

Mari Raja said…
Hi Charitha,

Thanks for reply.

Yes Charitha.I am using apache rampart module at my webservice.

In server side, I added my security policy on services.xml.

But in the client side, where can I add my security policy ?

This is my client code :

OMFactory omFactory = OMAbstractFactory.getOMFactory();
OMElement omSecurityElement = omFactory.createOMElement
(new QName( "", "Security", "wsse"), null);

OMElement omusertoken = omFactory.createOMElement
(new QName("", "UsernameToken", "wsu"), null);

OMElement omuserName = omFactory.createOMElement
(new QName("", "Username", "wsse"), null);

OMElement omPassword = omFactory.createOMElement
(new QName("", "Password", "wsse"), null);
omPassword.addAttribute("Type","",null );


But still I am getting the same exception. Please help me.

Mari Raja.
Charitha said…
If you are using a security policy at the service level, do not construct the security headers manually at the client side. Point to the relevant client policy as explained in



Options options = serviceClient.getOptions();
options.setProperty(RampartMessageData.KEY_RAMPART_POLICY, loadPolicy("C:\\wsas\\client-epo\\policy.xml"));

private static Policy loadPolicy(String xmlPath){
StAXOMBuilder builder = new StAXOMBuilder(xmlPath);
return PolicyEngine.getPolicy(builder.getDocumentElement());
Mari Raja said…
Thanks charitha. It is working fine. Thanks for ur help.

vishnu said…
Hi Charitha

I have somewhat different issue I am using Rampart for WS-Security and sign body using a signature given by third party and request message has security headers corresponding to it. But response doesn't have security headers which should be fine but Rampart complains about security headers not being found in response. If you encountered or know something about this could you please post the response. All your help is highly appreciated.

for your info stacktrace

[java] log4j:WARN No appenders could be found for logger (org.apache.axis2.deployment.FileSystemConfigurator).
[java] log4j:WARN Please initialize the log4j system properly.
[java] Exception in thread "main" org.apache.axis2.AxisFault: Missing wsse:Security header in request
[java] at org.apache.rampart.handler.RampartReceiver.setFaultCodeAndThrowAxisFault(
[java] at org.apache.rampart.handler.RampartReceiver.invoke(
[java] at org.apache.axis2.engine.Phase.invoke(
[java] at org.apache.axis2.engine.AxisEngine.invoke(
[java] at org.apache.axis2.engine.AxisEngine.receive(
[java] at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(
[java] at org.apache.axis2.description.OutInAxisOperationClient.send(
[java] at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(
[java] at org.apache.axis2.client.OperationClient.execute(
[java] at org.apache.axis2.client.ServiceClient.sendReceive(
[java] at org.apache.axis2.client.ServiceClient.sendReceive(
[java] at org.apache.rampart.samples.policy.sample03.Client.main(Unknown Source)
[java] Caused by: org.apache.rampart.RampartException: Missing wsse:Security header in request
[java] at org.apache.rampart.RampartEngine.process(
[java] at org.apache.rampart.handler.RampartReceiver.invoke(
[java] ... 10 more


Popular posts from this blog

Common mistakes to avoid in WSO2 ESB - 1 - "org.apache.axis2.AxisFault: The system cannot infer the transport information from the URL"

Working with HTTP multipart requests in soapUI

WS-Reliable messaging with WSO2 WSAS -1